Computing.Net > Forums > Unix > Hex to ascii

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Hex to ascii

Reply to Message Icon

Name: johan
Date: May 3, 2005 at 23:52:47 Pacific
OS: sun 5.9
CPU/Ram: 2048mb
Comment:

I have "one line file", for example:

48454C4C4F205747524C44

and it should be "one mark in line:
48
45
4C
e.t.c

or best of all, it should be converted from Hex to char:

HELLO WORDL

Thanks



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: May 4, 2005 at 12:31:31 Pacific
Reply:

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 ~


0

Response Number 2
Name: johan
Date: May 5, 2005 at 05:35:30 Pacific
Reply:

Thanks Nails


0

Response Number 3
Name: James Boothe
Date: May 5, 2005 at 11:14:54 Pacific
Reply:

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=2

while [ $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))
done

echo


0

Response Number 4
Name: nails
Date: May 5, 2005 at 11:31:01 Pacific
Reply:

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


0

Response Number 5
Name: James Boothe
Date: May 6, 2005 at 06:44:18 Pacific
Reply:

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.


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Hex to ascii

Hex to Decimal www.computing.net/answers/unix/hex-to-decimal/4017.html

convert a binary file to ascii www.computing.net/answers/unix/convert-a-binary-file-to-ascii/6213.html

binary to ASCII www.computing.net/answers/unix/binary-to-ascii/5502.html