Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have "one line file", for example:
48454C4C4F205747524C44
and it should be "one mark in line:
48
45
4C
e.t.cor best of all, it should be converted from Hex to char:
HELLO WORDL
Thanks

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.filestr="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
echoContents 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.
hexstring='48454C4C4F20574F524C44'
stringlen=$(expr length $hexstring)
cstart=1
cend=2while [ $cstart -lt $stringlen ]
do
hexchar=$(echo $hexstring | cut -c$cstart,$cend)
octchar=$(echo "ibase=16;obase=8;$hexchar"|bc)
decchar=$(echo "\0$octchar")
echo "$decchar\c"
((cstart=cstart+2))
((cend=cend+2))
doneecho

James:
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?
Regards,
Nails

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.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |