good chemistry is complicated, and a little bit messy -LW |
|
PerlMonks |
Re^5: How to convert binary to hexadecimalby roboticus (Chancellor) |
on Mar 22, 2013 at 01:14 UTC ( [id://1024850]=note: print w/replies, xml ) | Need Help?? |
Here's another way to do it:
It uses an old[1] trick: Since each "nybble"[2] represents a decimal value from 0 to 15, you can build a 16 character string of digits, and use the nybble value as the index of a character. Then, to simplify things, we ensure that the string is a multiple of four characters long. That way, we simply chop off four characters, turn it into a hex digit, repeating until the string is consumed. Notes: [1] How old? It was old when I learned it, roughly 35 years ago. [2] Four bits == half a byte, thus, a nybble. ...roboticus When your only tool is a hammer, all problems look like your thumb.
In Section
Seekers of Perl Wisdom
|
|