Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Perl Programming Language

Convert hex to bin


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)

Klaus Sulzberger (Klaus.Sulzber@de.bosch.com) wrote on MMMMCMXLI
September MCMXCIII in <URL:news:et3hku$oq7$1@news4.fe.internet.bosch.com>:
:}  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.

Well, don't do it then!

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

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"?

Abigail
--
#!/opt/perl/bin/perl -w
$\ = $"; $SIG {TERM} = sub {print and exit};
kill 15 => fork for qw /Just another Perl Hacker/;

On 12 Mar 2007 12:52:58 GMT, Abigail <abig@abigail.be> 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,

On Mon, 12 Mar 2007 13:48:16 +0100, "Klaus Sulzberger"

<Klaus.Sulzber@de.bosch.com> 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,

On 03/12/2007 07:48 AM, Klaus Sulzberger wrote:

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

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

On Mon, 12 Mar 2007 14:56:23 GMT, "Mumia W."

<paduille.4060.mumia.w+nos@earthlink.net> 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,

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

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

Michele Dondi <bik.m@tiscalinet.it> wrote in
news:op0bv2dnjr2tfudte5d01bcp7203vk2sl0@4ax.com:

> On Mon, 12 Mar 2007 14:56:23 GMT, "Mumia W."
> <paduille.4060.mumia.w+nos@earthlink.net> 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 <1@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
clpmisc guidelines: <URL:http://www.augustmail.com/~tadmc/clpmisc.shtml>

On 03/12/2007 12:49 PM, Mirco Wahab wrote:

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.

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

On Tue, 13 Mar 2007 10:04:08 GMT, "Mumia W."

<paduille.4060.mumia.w+nos@earthlink.net> wrote:
>>    sub hex2bin {
>>        base_convert (shift), 16, 2
>>    }
[snip]
>That's interesting. What module is "base_convert" a part of?

I thought it was a joke. Of course, I would have written it like

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

or

      sub hex2bin {
          base_convert shift, 16, 2;
      }

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,

Mumia W. wrote:
> 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

>>>>> "MW" == Mumia W <paduille.4060.mumia.w+nos@earthlink.net> writes:

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

  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

--
Uri Guttman  ------  u@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org

Uri Guttman wrote:
> 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

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc