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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
convert text $var to hex



Dec 27, 2000, 12:48 AM

Post #1 of 3 (276 views)
convert text $var to hex Can't Post

I have an ascii string (potentially null thru 255) that I want to convert so it shows as the equivalent hex format in $var...

>> ascii example: qwertyzz
>> hex equivalent: 77 65 72 74 79 7a 7a
(spaces between hex values added for clarity)

I have beat my head against PACK - UnPACK
tried HEX, ORD, whatever...
I cannot find any combo of anything that will end up with the Hex equiv in a $variable string.
I'm sure there is an easy way. Docs on Pack-UnPack are pretty basic and never cover odd ball stuff like this.

Thanks. Mike


Dec 27, 2000, 6:44 AM

Post #2 of 3 (275 views)
Re: convert text $var to hex [In reply to] Can't Post

$var = 'qwertyzz';
$hex = '';
$hex .= sprintf "%x", ord substr $var, $_, 1 for (0..length($var)-1);



Dec 27, 2000, 9:18 AM

Post #3 of 3 (272 views)
Re: convert text $var to hex [In reply to] Can't Post

Here are some solutions:

$str = "ABC"; 

$hex = unpack "H*", $str;
# $hex = '414243'

@parts = unpack "H2" x length($str), $str;
# @parts = (41,42,43)

$hex = sprintf "x" x length($str), map ord, split //, $str;
# $hex = '414243'

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


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

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