hex to binary

  • Thread starter Venkatesh can....can...
  • Start date
J

Jürgen Exner

Venkatesh can....can... said:
How to convert hexadecimal number to binary in perl ??

You must have missed the current discussion titled "decrease MAC".
There are no hexadicimal numbers but only numbers in hexadecimal
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
 
B

benkasminbullock

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/8).int(($i/4)%2).int(($i/
2)%2).int($i%2);
}

# Now test

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top