Velocity Reviews > Perl > hex to binary

# hex to binary

Venkatesh can....can...
Guest
Posts: n/a

 03-18-2008
How to convert hexadecimal number to binary in perl ??

Josef Moellers
Guest
Posts: n/a

 03-18-2008
Venkatesh can....can... wrote:
> How to convert hexadecimal number to binary in perl ??

perldoc -f hex

--
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html

Jürgen Exner
Guest
Posts: n/a

 03-18-2008
"Venkatesh can....can..." <(E-Mail Removed)> wrote:
>How to convert hexadecimal number to binary in perl ??

You must have missed the current discussion titled "decrease MAC".
representation. And the same for binary.
So, use hex() to convert the string into a number and then sprintf() to
convert the number into a binary representation as string.

jue

benkasminbullock@gmail.com
Guest
Posts: n/a

 03-19-2008
On Mar 18, 8:37 pm, "Venkatesh can....can..."
<(E-Mail Removed)> wrote:
> How to convert hexadecimal number to binary in perl ??

#! perl
use warnings;
use strict;

# Create the stuff for matching

my \$matches = "0123456789ABCDEF";
my %hex2bin;
for (my \$i=0;\$i<16;\$i++) {
\$hex2bin{substr(\$matches,\$i,1)}=int(\$i/.int((\$i/4)%2).int((\$i/
2)%2).int(\$i%2);
}

# Now test

print "Before: \$hex_string\n";
\$hex_string =~ s/([\$matches])/\$hex2bin{uc(\$1)}/gi;
print "After: \$hex_string\n";