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.";
|