Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Convert to Hexadecimal

by sauoq (Abbot)
on May 17, 2012 at 19:00 UTC ( [id://971151]=note: print w/replies, xml ) Need Help??


in reply to Convert to Hexadecimal

As davido points out, sprintf() would be almost good enough on its own.

$ perl -le 'printf( "%04x\n", 12)' 000c

And, as he points out, you can get the rest of the way with s///. That's pretty ugly though. You could also get there a few other ways... but they are all pretty ugly.

It would be nice if you could have something like

perl -le 'printf( "\\x%02x\\x%02x\n", 12 )' # WRONG \x0c\x00

Unfortunately, "12" is one argument, not two. But that's where unpack comes in handy. You want to unpack a number into two (char sized) values. That leads you to

perl -le 'printf( "\\x%02x\\x%02x\n", unpack( "C2", 12 ))' WRONG \x31\x32

But that's wrong too as 12 is a scalar and gets unpacked (by that) as "1", "2" (ASCII 0x31 and 0x32 respectively.) And this is where pack is useful. You want to pack that 12 into a single value before unpacking it into two. Two characters is a short...

perl -le 'printf( "\\x%02x\\x%02x\n", unpack( "C2", pack("S", 12)))' \x0c\x00

Oops. Still not quite right. My architecture is little-endian (x86). But that's okay, we can force pack to give us a big-endian short.

perl -le 'printf( "\\x%02x\\x%02x\n", unpack( "C2", pack("n", 12)))' \x00\x0c # Another way to say that . . . perl -le 'printf( "\\x%02x\\x%02x\n", unpack( "C2", pack("S>", 12)))' \x00\x0c

It might not be beautiful, but I think it's better than futzing about with string operations after the conversion to hex. Of course, if you read perlpacktut as suggested, I'm sure you've come to this solution already! ;-)

-sauoq
"My two cents aren't worth a dime.";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://971151]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2024-04-19 13:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found