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
}
'