There might be an easier way but I read asc.file, contents at the bottom, which is the hex representation and it's associated character representation. Created a variable for each. I then parsed your string two characters at atime, created my variable and printed it out.
#!/bin/ksh
while read c1 c2 do eval a$c1=\$c2 done < asc.file
str="48454C4C4F205747524C44" len=$(expr "$str" : '.*') # length of str
# no error checking! s1=1 while (($s1 < $len)) do x=$(echo "$str"|awk ' { print substr($0, '"$s1"', 2) } ') ch=$(eval echo \"\$a$x\") if [[ $ch = "SP" ]] then # space is special printf " " $ch else printf "%s" $ch fi ((s1+=2)) # bump counter by 2 done echo
Contents of ascfile:
20 SP 21 ! 22 " 23 # 24 $ 25 % 26 & 28 ( 29 ) 2A * 2B + 2C , 2D - 2E . 30 0 31 1 32 2 33 3 34 4 35 5 36 6 38 8 39 9 3A : 3B ; 3C < 3D = 3E > 40 @ 41 A 42 B 43 C 44 D 45 E 46 F 47 G 48 H 49 I 4A J 4B K 4C L 4D M 4E N 4F O 50 P 51 Q 52 R 53 S 54 T 55 U 56 V 57 W 58 X 59 Y 5A Z 5B [ 5C \ 5D ] 5E ^ 60 ` 61 a 62 b 63 c 64 d 65 e 66 f 68 h 69 i 6A j 6B k 6C l 6D m 6E n 6F o 70 p 71 q 72 r 73 s 74 t 75 u 76 v 78 x 79 y 7A z 7B { 7C | 7D } 7E ~
Here's a solution that uses bc to convert each hex value to octal, then uses echo to display the characters via their octal values. The \c supresses newline, so the resulting character string is displayed on a single line.
That's a nice bc solution. It works well on my Solaris 7 box as long as I use my original method for determining string length. 'expr length' doesn't work in my world.
My expr MAN page says the length keyword was an extension from INTERACTIVE UNIX. What unix are you using?
Thanks for the feedback Nails. I use the length construct on HP-UX and AIX. And I do not have access to linux at the moment, but I'm pretty sure it works there also.
The information on Computing.Net is the opinions of its users. Such
opinions may not be accurate and they are to be used at your own risk.
Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE