CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Converting from Hex to Char

 



Troy
Novice

May 21, 2002, 8:56 AM

Post #1 of 4 (444 views)
Converting from Hex to Char Can't Post

I need to convert a file from 2-digit Hex representation to its ASCII Character (Full table 256 codes) representation.
Does anyone know of a function available to do this for me. I then need to save the data as a binary file.

Thanks - Troy


mhx
Enthusiast / Moderator

May 22, 2002, 11:52 AM

Post #2 of 4 (439 views)
Re: [Troy] Converting from Hex to Char [In reply to] Can't Post

One way to do it is to use pack:


Code
binmode STDOUT; 
print pack 'H*', <>;


The program reads from standard input and writes to standard output. However, that solution requires the input file to have no extra white space in between or any bytes padding the file at the end.

A more "reliable" solution would be:


Code
binmode STDOUT; 
while(<>) {
print map { chr hex } /[[:xdigit:]]{2}/g
}


This solution allows any kind of "extra" white space in between the hex characters.
The binmode is required on platforms that make a difference between binary and ascii files, like Windows.

Hope this helps.

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



Troy
Novice

May 23, 2002, 7:48 AM

Post #3 of 4 (428 views)
Re: [mhx] Converting from Hex to Char [In reply to] Can't Post

So how would I use this snippet of code to convert say 057 Hex to the ASCII Character "W"
Eg.
In my code I have:
$data_hex = '057';

and I want to end up with
$data_ascii = 'W';

Thanks, Troy


mhx
Enthusiast / Moderator

May 23, 2002, 8:06 AM

Post #4 of 4 (427 views)
Re: [Troy] Converting from Hex to Char [In reply to] Can't Post

Well, in your original post you asked how to convert 2-digit hex values to ASCII. Also, you wanted to convert one file to another. Now you have a 3-digit hex value stored in a scalar and want to convert it to a scalar holding the corresponding ASCII character. Except for the fact of converting hex to ASCII, these are completely different tasks.

However, what you want to do can be accomplished with the chr and hex functions:


Code
$data_hex = '057'; 
$data_ascii = chr hex $data_hex;


The [url=http://www.perldoc.com/perl5.6.1/pod/func/hex.html]hex function interprets a string as a hexadecimal value and returns its decimal representation. The [url=http://www.perldoc.com/perl5.6.1/pod/func/chr.html]chr function then takes that decimal value and converts it to the corresponding ASCII character. You can follow the links to get to the online manpages of these function.

Hope this helps.

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives