Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

Hex to ascii

Original Message
Name: johan
Date: May 3, 2005 at 23:52:47 Pacific
Subject: Hex to ascii
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


Report Offensive Message For Removal


Response Number 1
Name: nails
Date: May 4, 2005 at 12:31:31 Pacific
Subject: Hex to ascii
Reply: (edit)
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 ~


Report Offensive Follow Up For Removal

Response Number 2
Name: johan
Date: May 5, 2005 at 05:35:30 Pacific
Subject: Hex to ascii
Reply: (edit)
Thanks Nails

Report Offensive Follow Up For Removal

Response Number 3
Name: James Boothe
Date: May 5, 2005 at 11:14:54 Pacific
Subject: Hex to ascii
Reply: (edit)
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


Report Offensive Follow Up For Removal

Response Number 4
Name: nails
Date: May 5, 2005 at 11:31:01 Pacific
Subject: Hex to ascii
Reply: (edit)
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


Report Offensive Follow Up For Removal

Response Number 5
Name: James Boothe
Date: May 6, 2005 at 06:44:18 Pacific
Subject: Hex to ascii
Reply: (edit)
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.


Report Offensive Follow Up For Removal




Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Hex to ascii

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




XP Installed to G?

exessive internet traffic

ZoneAlarm Question. Blocked Connect

Windows Live Messenger Problem

Delete $Uninstall after SP3 updates


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

All content ©1996-2007 Computing.Net, LLC