Sujet : Re: The joy of octal
De : 186283 (at) *nospam* ud0s4.net (186282@ud0s4.net)
Groupes : comp.os.linux.miscDate : 15. Nov 2024, 08:58:55
Autres entêtes
Organisation : wokiesux
Message-ID : <UKScnT53YMTJYqv6nZ2dnZfqnPWdnZ2d@earthlink.com>
References : 1 2 3 4
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0
On 11/12/24 5:45 PM, Fritz Wuehler wrote:
Chris Ahlstrom <OFeem1...@teleworm.us> [CA]:
CA> I still haven't found a program that would easily reverse
CA> octal to a string.
You have to take into account the writer machine's endianness
and character set encoding (EBCDIC anyone?).
If it's a big-endian system and the encoding is ASCII, use the
following script. It preserves regular text and converts only what
it sees as octal numbers.
Modifying it to also accept EBCDIC encoded text on a non-EBCDIC
system is left as an exercise for the reader.
I would assume that if you could find a bourne shell, 'sed' and 'awk'
on some EBCDIC system, the script might work without changes.
Can anyone comment on this?
#!/bin/sh
DELIMITER="__octal_number_delimiter__" ;
# Step1: locate octal numbers in input
sed 's@\b\([0-7 ]\+\)\b@'"$DELIMITER"'\1'"$DELIMITER"'@g' |
# Step2: convert octal numbers to text, leaving everything else as it is
awk -v FS="$DELIMITER" -v Q='"' '{
for (i=1;i<=NF;i++) { # for all line fields
t = $i; # get the i-th line field
if (i%2) { # regular text; do not touch it
printf "%s", t;
} else { # octal number; process it
gsub(" ","", t); # remove any space characters first
# make sure the number of octal digits is a mulitiple of 6
for (j=1;j<=(length(t)%6);j++) t = "0" t;
# break up the octal string to sextuplets
for (j=1;j<=length(t)/6;j++) {
# convert a 6-digit octal number to a 4-digit hex number
six_octal_digits=substr(t,6*j-5,6);
four_hex_digits = sprintf ("%x", strtonum(0 six_octal_digits));
# process each pair of hex digits (a byte)
for (k=1;k<=length(four_hex_digits)/2;k++) {
two_hex_digits=substr(four_hex_digits,2*k-1, 2);
# output this byte as an ASCII character (escape non-printable ones)
char = strtonum("0x" two_hex_digits);
printf (((char < 32) ? "<%02x>" : "%c"), char);
}
}
}
}
print ""; # add a newline
}
'
Wow ... haven't thought about octal/EBCDIC for
a LONG time.
How long since 12-bit processors ?
Well, they WERE great at the time ...
For SOME apps today 12-bit would STILL
be good.
I think some PICs were/are effectively 12-bit.