Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Convert hex to bin

Reply
Thread Tools

Convert hex to bin

 
 
Klaus Sulzberger
Guest
Posts: n/a
 
      03-12-2007
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)


 
Reply With Quote
 
 
 
 
Michele Dondi
Guest
Posts: n/a
 
      03-12-2007
On 12 Mar 2007 12:52:58 GMT, Abigail <(E-Mail Removed)> wrote:

>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
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
Reply With Quote
 
 
 
 
Michele Dondi
Guest
Posts: n/a
 
      03-12-2007
On Mon, 12 Mar 2007 13:48:16 +0100, "Klaus Sulzberger"
<(E-Mail Removed)> wrote:

>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
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
Reply With Quote
 
Mumia W.
Guest
Posts: n/a
 
      03-12-2007
On 03/12/2007 07:48 AM, Klaus Sulzberger wrote:
> 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.



 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      03-12-2007
On Mon, 12 Mar 2007 14:56:23 GMT, "Mumia W."
<(E-Mail Removed)> wrote:

> sub hex2bin {
> unpack 'B8', pack 'H2', $_[0];
> }
>
>The "perldoc -f pack" command describes the format strings.


Naaah, you spoon-fed him at last!


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      03-12-2007
Klaus Sulzberger wrote:
> 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
 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      03-12-2007
Mumia W. wrote:
>
> sub hex2bin {
> unpack 'B8', pack 'H2', $_[0];
> }
>


another possibility would be:

...

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

...



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

Regards

Mirco
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      03-13-2007
Michele Dondi <(E-Mail Removed)> wrote in
news(E-Mail Removed):

> On Mon, 12 Mar 2007 14:56:23 GMT, "Mumia W."
> <(E-Mail Removed)> wrote:
>
>> 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

--
A. Sinan Unur <(E-Mail Removed)>
(remove .invalid and reverse each component for email address)
clpmisc guidelines: <URL:http://www.augustmail.com/~tadmc/clpmisc.shtml>

 
Reply With Quote
 
Mumia W.
Guest
Posts: n/a
 
      03-13-2007
On 03/12/2007 12:49 PM, Mirco Wahab wrote:
> Mumia W. wrote:
>>
>> 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.

 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      03-13-2007
Mumia W. wrote:
> On 03/12/2007 12:49 PM, Mirco Wahab wrote:
>> another possibility would be:
>> ...
>>
>> sub hex2bin {
>> base_convert (shift), 16, 2
>> }
>>
>> ...
>> (OK, I said "possibility") ...

>
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
#!/usr/bin/ruby , #!/usr/bin/ruby -w , #!/usr/bin/ruby -T?, #!/usr/bin/ruby -T1... anne001 Ruby 1 04-23-2006 03:02 PM
Hex Color Codes - Hex 6 <=> Hex 3 lucanos@gmail.com HTML 10 08-18-2005 11:21 PM
Convert HEX string to bin GIR C Programming 53 12-10-2003 05:18 AM
How to convert an hex string to a Hex number chirs Javascript 3 12-01-2003 10:06 PM
hex(-5) => Futurewarning: ugh, can't we have a better hex than '-'[:n<0]+hex(abs(n)) ?? Bengt Richter Python 6 08-19-2003 07:33 AM



Advertisments