Convert hex to bin

K

Klaus Sulzberger

Follwing function convert Hex to bin:

sub hex2bin
{
my $str=unpack("B32",pack("N",hex$_[0]));
$str=~s/^0+(?=\d)//;
return($str);
}

The problem of the function is, that the result cuts the zeros.

For examples:

Hex: 7D
Function result: 1111101
Want to have: 01111101
(for 1 Byte)
 
M

Michele Dondi

Is your posting the Perl equivalent of the joke of the man going to
the doctor saying "my head hurts when I bang it against the wall"?

I think he found the function ready made, and hardly grasps what it
does. In which case he should have said so. This is not a helpdesk
anyway. ++ for the fun!


Michele
 
M

Michele Dondi

sub hex2bin
{
my $str=unpack("B32",pack("N",hex$_[0]));
$str=~s/^0+(?=\d)//;
return($str);
}

The problem of the function is, that the result cuts the zeros.

Putting '#' (without quotes) in front of one of those lines will do
what you want. Since the lines are three, you may find the right one
by trial. Incidentally, once you have done so you may avoid the $str
variable altogether, and the sub may consist of a single statement.
This is left as an exercise, of course.


Michele
 
M

Mumia W.

Follwing function convert Hex to bin:

sub hex2bin
{
my $str=unpack("B32",pack("N",hex$_[0]));
$str=~s/^0+(?=\d)//;
return($str);
}

The problem of the function is, that the result cuts the zeros.

For examples:

Hex: 7D
Function result: 1111101
Want to have: 01111101
(for 1 Byte)
sub hex2bin {
unpack 'B8', pack 'H2', $_[0];
}

The "perldoc -f pack" command describes the format strings.
 
M

Michele Dondi

sub hex2bin {
unpack 'B8', pack 'H2', $_[0];
}

The "perldoc -f pack" command describes the format strings.

Naaah, you spoon-fed him at last! :)


Michele
 
M

Mirco Wahab

Klaus said:
sub hex2bin
{
my $str=unpack("B32",pack("N",hex$_[0]));
$str=~s/^0+(?=\d)//;
return($str);
}

The problem of the function is, that the result cuts the zeros.
For examples:
Hex: 7D
Function result: 1111101
Want to have: 01111101
(for 1 Byte)

Aside from the fun part, you
can of course 'hand craft' such
a functionality *without any*
of Perl's appropriate builtins,
like:

...
sub hax2bin {
my $BS = {
0=>'OOOO', 1=>'OOOL', 2=>'OOLO', 3=>'OOLL' ,
4=>'OLOO', 5=>'OLOL', 6=>'OLLO', 7=>'OLLL' ,
8=>'LOOO', 9=>'LOOL', A=>'LOLO', B=>'LOLL' ,
C=>'LLOO', D=>'LLOL', E=>'LLLO', F=>'LLLL'};
join ':', map $BS->{ $_ }, split //, $1
if (shift) =~ / (?:0x)? (.+) /x
}

my $hnum = '0xA1A2';
my $bnum = hax2bin $hnum;
print $bnum;
...

if you really need 'very special' mappings.

Regards

Mirco
 
M

Mirco Wahab

Mumia said:
sub hex2bin {
unpack 'B8', pack 'H2', $_[0];
}

another possibility would be:

...

sub hex2bin {
base_convert (shift), 16, 2
}

...



(OK, I said "possibility") ...

Regards

Mirco
 
A

A. Sinan Unur

sub hex2bin {
unpack 'B8', pack 'H2', $_[0];
}

The "perldoc -f pack" command describes the format strings.

Naaah, you spoon-fed him at last! :)

Oh, but that's what he does.

Anyway, did I understand the question correctly? One has something like:

my $s = 'deadbeaf';

and wants its binary representation. Why go through all the contortions
with pack/unpack?

C:\Home\asu1> perl -e "printf qq{%32.32b\n}, hex 'deadbeef'"
11011110101011011011111011101111

Sinan
 
M

Mumia W.

Mumia said:
sub hex2bin {
unpack 'B8', pack 'H2', $_[0];
}

another possibility would be:

...

sub hex2bin {
base_convert (shift), 16, 2
}

...



(OK, I said "possibility") ...

Regards

Mirco


That's interesting. What module is "base_convert" a part of?

Yahoo-ing online, I've been able to find a base_convert() for PHP, but
not for Perl.
 
M

Mirco Wahab

Mumia said:
That's interesting. What module is "base_convert" a part of?
Yahoo-ing online, I've been able to find a base_convert() for PHP, but
not for Perl.

I wrote this because *I think* such
a simple number base conversion
'core functionality' is missing
from Perl (but is available in
PHP).

There is a module Math::BaseCalc
which appears to do that - but
doesn't work that intuitive (no core).

The interface would be like (fictional):

use Numbers::Base qw 'convert'; # core

sub my_hex2bin {
convert (shift), 16, 2
}


Maybe there is already something
appropriate I missed? Perl6?

Regards

Mirco
 
M

Mirco Wahab

Mumia said:
That's interesting. What module is "base_convert" a part of?

Yahoo-ing online, I've been able to find a base_convert() for PHP, but
not for Perl.

I wrote this because *I think* such
a simple number base conversion
'core functionality' is missing
from Perl (but is available in
PHP).

There is a module Math::BaseCalc
which appears to do that - but
doesn't work that intuitive (no core).

The interface would be like (fictional):

use Numbers::Base qw 'convert'; # core

sub my_hex2bin {
convert shift, 16, 2
}


Maybe there is already something
appropriate I missed? Perl6?

Regards

Mirco
 
U

Uri Guttman

MW> That's interesting. What module is "base_convert" a part of?

MW> Yahoo-ing online, I've been able to find a base_convert() for PHP, but
MW> not for Perl.

check Math::BaseCnv (on cpan) and its cnv() function. it will convert
any base number to any base and also do some smart guessing for you. and
it can export some common variation subs too.

i just discovered it while helping a client with hex to base36
conversions.

as for having general base conversions in core, let php have the
bloat. perl already can convert between binary, octal, decimal and hex
in core and in multiple ways (sprintf, hex, oct, pack/unpack). the rest
should be a module as it is.

uri
 
M

Mirco Wahab

Uri said:
check Math::BaseCnv (on cpan) and its cnv() function. it will convert
any base number to any base and also do some smart guessing for you. and
it can export some common variation subs too.

Thanks for that hint (why didn't I find it?) ...
i just discovered it while helping a client with
hex to base36 conversions.
Good.

as for having general base conversions in core, let php have
the bloat. perl already can convert between binary, octal,
decimal and hex in core and in multiple ways (sprintf, hex,
oct, pack/unpack). the rest should be a module as it is.

OK, you like it as it is. But to have a "central
conversion" location in core, which does the obvious
things (hex, oct etc.) if possible and "digs deep"
if necessary - wouldn't be that bad imho.

With this in mind, the OP could have used
for example

...

use Math::BaseCnv;

sub base_convert {
cnv @_
}

my $hnum = '7D';
my $bnum = base_convert $hnum, 16, 2;

printf "%0*s", 4*length($hnum), $bnum;

...


Regards & thanks

Mirco
 

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

No members online now.

Forum statistics

Threads
474,125
Messages
2,570,749
Members
47,302
Latest member
MitziWragg

Latest Threads

Top