laziness, impatience, and hubris | |
PerlMonks |
Re: Convert to Hexadecimalby sauoq (Abbot) |
on May 17, 2012 at 19:00 UTC ( [id://971151]=note: print w/replies, xml ) | Need Help?? |
As davido points out, sprintf() would be almost good enough on its own.
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
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
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...
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.
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.";
In Section
Seekers of Perl Wisdom
|
|