Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

How to convert binary to hexadecimal

by vaas_m (Initiate)
on Oct 11, 2007 at 13:32 UTC ( #644225=perlquestion: print w/replies, xml ) Need Help??
vaas_m has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am having a file with binary data. I want to convert it into hexadecimal data. Can anybody know a direct way of doing it without hashes or without coverting to decimal. Any perl modules that I can use to do it. Thanx, Srini

Replies are listed 'Best First'.
Re: How to convert binary to hexadecimal
by Corion (Pope) on Oct 11, 2007 at 13:36 UTC

    What have you tried so far?

    The perlfaq4 lists various conversions, for example from binary to decimal and from decimal to hexadecimal. Why don't you combine those two steps to form a direct conversion from binary to hexadecimal?

    I found the answers by using

    perldoc -q convert
Re: How to convert binary to hexadecimal
by ikegami (Pope) on Oct 11, 2007 at 13:49 UTC

    That's a totally pointless requirement, but the following does achieve it:

    $hex = sprintf('%X', oct("0b$bin"));

    oct converts the binary representation of the number to a number, then sprintf '%X' converts it to its hex representation. It's never represented in decimal.

    Update: Or maybe you're actually interested in converting bytes to their hex representation:

    $hex = sprintf('%02X', ord($byte));
Re: How to convert binary to hexadecimal
by Krambambuli (Curate) on Oct 11, 2007 at 13:50 UTC
    Before (maybe mis-)interpreting what a 'file with binary data' might be, I'd better ask... :)

    Is it a binary file with fixed-size records?
    Is it a text file containing strings like '100000111...11' ?
    Are there any known limitations on how long the binary inputs and expected values can be ?

    Those additional questions might guide to totally different solutions.
Re: How to convert binary to hexadecimal
by zentara (Archbishop) on Oct 11, 2007 at 15:28 UTC
    Update: a decimal in there. Sorry.

    Another snippet for your perusal:

    #!/usr/bin/perl use warnings; use strict; my $binary = '11101010101010101010101010101010'; my $int = unpack("N", pack("B32", substr("0" x 32 . $binary, -32))); my $hex = sprintf("%x", $int ); print $hex,"\n";

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      Hi zentara and all, Thanx for the info. I got it done very easily using pack and unpack as suggested by zentara. Rgds, Kiran Polu
        THanks Ikigami . I was looking everywhere for that.
      this function works only for 32 bits, this will work for any binary length sub b2h { my $num=shift; my $WIDTH=32; my $index=length($num)-$WIDTH; print "index= $index\n"; my $hex=""; do { my $width=$WIDTH; if ($index<0) { $width+=$index; $index=0; } $cut_string=substr($num,$index,$width); print "index is $index width is $width Cut String is $cut_string\n"; $hex= sprintf('%X', oct("0b$cut_string")). $hex; $index-=$WIDTH; } while ($index>(-1*$WIDTH)); return $hex; }
        Since the code Does What I Want (works even for bit widths > 32), I have made it readable by using perltidy. I have also made it strict and removed the print statements:
        use warnings FATAL => 'all'; use strict; for ( qw( 00 1010 1110 010001 01101011 ), '1' x 33, '0101' x 16 ) { my $h = b2h($_); print "$_ $h width=", length($_), "\n"; } exit; sub b2h { my $num = shift; my $WIDTH = 32; my $index = length($num) - $WIDTH; my $hex = ''; do { my $width = $WIDTH; if ($index < 0) { $width += $index; $index = 0; } my $cut_string = substr($num, $index, $width); $hex = sprintf('%X', oct("0b$cut_string")) . $hex; $index -= $WIDTH; } while ($index > (-1 * $WIDTH)); return $hex; } __END__ Output: 00 0 width=2 1010 A width=4 1110 E width=4 010001 11 width=6 01101011 6B width=8 111111111111111111111111111111111 1FFFFFFFF width=33 0101010101010101010101010101010101010101010101010101010101010101 55555 +55555555555 width=64

        I have also been attempting to implement this with pack and unpack, to no avail.

Re: How to convert binary to hexadecimal
by roboticus (Chancellor) on Oct 11, 2007 at 14:11 UTC

    You don't even need perl for this one (at least on a Unix / Cygwin box):

    od -t x1 input_file


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://644225]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2017-06-27 12:38 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (605 votes). Check out past polls.