Re: How to convert binary to hexadecimal
by Corion (Patriarch) 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
| [reply] [d/l] |
Re: How to convert binary to hexadecimal
by ikegami (Patriarch) 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));
| [reply] [d/l] [select] |
Re: How to convert binary to hexadecimal
by zentara (Archbishop) on Oct 11, 2007 at 15:28 UTC
|
#!/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";
| [reply] [d/l] |
|
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
| [reply] |
|
THanks Ikigami .
I was looking everywhere for that.
| [reply] |
|
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;
}
| [reply] |
|
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. | [reply] [d/l] |
|
|
|
|
|
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.
| [reply] |
Re: How to convert binary to hexadecimal
by roboticus (Chancellor) on Oct 11, 2007 at 14:11 UTC
|
| [reply] [d/l] |