http://www.perlmonks.org?node_id=992924


in reply to array of binaries to hex conversion

$ 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

Replies are listed 'Best First'.
Re^2: array of binaries to hex conversion
by bebe (Novice) on Sep 11, 2012 at 06:57 UTC

    Perfect!! thanks a lot!!!!

Re^2: array of binaries to hex conversion
by remiah (Hermit) on Sep 12, 2012 at 01:41 UTC

    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.

        Thanks for reply. Stories of bit is a little bit hard for me. Please point out if I am saying something wrong.

        How/what?

        I was wrong. I was confused with little endian and big endian. Here OP expects it's input bit array as Big Endian(N) and descending bit order(B).

        printf "0x%x\n",                #hex
            unpack("N",                 #unpack it as 32bit int ,big endian
                pack("B32",             #pack it as MSB descending bit, 32bit inte
                    sprintf("%032s",    #32 length bit string
                        join('',
                            @rand1))));
        
        
        So, left pad works fine, even if it was an array of 16bit.
        my $num;
        $num=pack('n',7); #16bit short Big endian, it's value is 7
        printf "big endian, descending bit order:%s\n", unpack('B16',$num);
        printf "big endian, ascending  bit order:%s\n", unpack('b16',$num);
        printf "unsigned short=%d\n",unpack('n',pack('B16', unpack('B16', $num)));
        
        printf "right pad:%s\n", unpack('B16',$num) . '0' x 16;
        printf "32bit right pad=%d\n",unpack('N',pack('B32', unpack('B16',$num) . '0' x 16));
        #==> this prints "32bit right pad=458752"
        printf "left  pad:%s\n", '0' x 16 . unpack('B16',$num);
        printf "32bit left  pad=%d\n",unpack('N',pack('B32', sprintf('%032d', unpack('B16',$num)))
        #==> this prints "32bit left  pad=7"
        
        You are the same anonymous with this.
        print  '0x'. unpack 'H*', pack("B32", substr("0" x 32 . join('',@rand1), -32));
        
        So, you are going to say: If you need it's hexadecimal values, there is no need for decimal( unpack 'N') -> hexadecimal conversion( printf %x), just print it as hex( unpack 'H').

        I am going to see your links. Thanks for reply.