Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

array of binaries to hex conversion

by bebe (Novice)
on Sep 11, 2012 at 03:28 UTC ( [id://992911]=perlquestion: print w/replies, xml ) Need Help??

bebe has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, could you please get some help with the code below. It works but I think I'm doing this conversions very inefficiently, I was wondering if you have other suggestions or better alternatives I could use. I appreciate it all. :-)

# rand1 is array of binaries # basically, I want to convert array of binaries to hex string my $bin = arraystring (@rand1); my $dec = stringdecimal($bin); my $hex = sprintf("0x%x", $dec); sub stringdecimal { return unpack("N", pack("B32", substr("0" x 32 . shift, -32))); } sub arraystring { my $string = join('', @_); return $string; }

Replies are listed 'Best First'.
Re: array of binaries to hex conversion
by jwkrahn (Abbot) on Sep 11, 2012 at 05:49 UTC
    $ perl -le' my @rand1 = qw( 0 1 0 1 0 1 0 1 0 1 1 1 0 1 1 0 0 0 1 ); sub stringdecimal { return unpack("N", pack("B32", substr("0" x 32 . shift, -32))); } sub arraystring { my $string = join("", @_); return $string; } my $bin = arraystring (@rand1); my $dec = stringdecimal($bin); my $hex = sprintf("0x%x", $dec); print $hex; ' 0x2abb1 $ perl -le' my @rand1 = qw( 0 1 0 1 0 1 0 1 0 1 1 1 0 1 1 0 0 0 1 ); my $hex = sprintf "0x%x", oct "0b" . join "", @rand1; print $hex; ' 0x2abb1

      Perfect!! thanks a lot!!!!

      Hello jwkrahn

      I have just read perlpacktut and I would like to have some clue for my 2 questions about this thread.

      What OP does is in short...

      printf "0x%x\n", #print it as hex unpack("N", #unpack it as 32bit int ,big endian pack("B32", #pack it as MSB 32bit integer sprintf("%032s", #32 length bit string join('', @rand1))));
      1. Does it really MSB ?

      MSB should pad '0' for right side, not left side, doesn't it ?
      my @rand1 = qw( 1 );
      
      this currenty goes "00000000000000000000000000000001". Is it OK?

      2. How oct insures it is MSB and 32bit big endian?

      Does oct thinks it as 'long long' seeing the size of $Config{longlongsize}? How it thinks for MSB?
      use Config; print "int size=".$Config{intsize}."\n"; print "short size=".$Config{shortsize}."\n"; print "long size=".$Config{longsize}."\n"; print "long long size=".$Config{longlongsize}."\n";
      If I could have some explanations by you or other monks, I am glad.

Re: array of binaries to hex conversion
by Anonymous Monk on Sep 11, 2012 at 07:02 UTC
    #!/usr/bin/perl -- use strict; use warnings; use Data::Dump; my @rand1 = qw( 0 1 0 1 0 1 0 1 0 1 1 1 0 1 1 0 0 0 1 ); my $bin = arraystring (@rand1); my $dec = stringdecimal($bin); my $hex = sprintf("0x%x", $dec); my $ex = b32hex(@rand1); dd \@rand1, $bin, $dec, $hex, $ex; sub stringdecimal { return unpack("N", pack("B32", substr("0" x 32 . shift, -32))); } sub arraystring { my $string = join('', @_); return $string; } sub b32hex { '0x'. unpack 'H*', pack("B32", substr("0" x 32 . join('',@_), -32) +) } __END__ ( [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1], "0101010101110110001", 175025, "0x2abb1", "0x0002abb1", )

      Hello.

      I would like to ask you a question. The result will differ by byte orders.

      my $tmp=pack("B32", substr("0" x 32 . join('',@rand1), -32)); #this prints 'as 32bit big endian:0x2abb1' printf "as 32bit big endian:0x%x\n", unpack('N',$tmp) ; #this prints 'as 32bit little endian:0x200' printf "as 32bit little endian:0x%x\n", unpack('S',$tmp) ;
      There is "<" for little endian, and ">" for big endian, which pack,unpack offers. OP treats it as "N"=32bit int, big endian, so I would like to do like this.
      pack("B>32", $bitstring);
      pack("(B32)>*",$bitstring);
      
      But both of them doesn't work...
      How can I add endian specification with your one line conversion? Is there a good way?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://992911]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-25 20:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found