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

I've got a four byte header in a binary file as the length of said file. The header is 641e 0100 and is a 32 bit little endian value.

The output should be (decimal) 73316, and after a while playing around with pack and unpack, i cant work out how to convert the 641e 0100 to 73316!

Any help appreciated!

Replies are listed 'Best First'.
Re: hex to binary
by AnomalousMonk (Archbishop) on Feb 17, 2014 at 04:45 UTC

    0x11e64 == 73316, but as others have asked, what's the raw byte order you are given and what unpack templates have you tried?

    Update:

    c:\@Work\Perl>perl -wMstrict -le "my $raw = pack 'H*', '641e0100'; my $le32 = unpack 'V', $raw; printf qq{%d decimal 0x%x hex \n}, $le32, $le32; " 73316 decimal 0x11e64 hex

      The raw bytes out of the file are 641e0100. TBH, i have really no clue right now and have tried the following:
      perl -le 'print unpack("N", pack("H8",substr("0"x8 . "641e0100",-8))); perl -le 'print unpack("N", pack("H8",substr("0"x8 . "641e0100",-8))); +' perl -le 'print unpack("N", pack("H8",substr("0"x8 . "641e01",-8)));' perl -e "print hex('641e0100')" perl -e "print hex('641e01')" perl -e "print unpack('V',pack 'V', hex('641e01'))" perl -e "print unpack('V', '0x641e01')" perl -e "print unpack('N', '0x641e01')" perl -e "print unpack('n', '0x641e01')" perl -e "print unpack('h', '0x641e0100')" perl -e "print unpack('h*', '0x641e0100')" perl -e "print pack('h*', '0x641e0100')"

        Please see update to my original reply.

Re: hex to binary ( UInt32 / Int32 )
by Anonymous Monk on Feb 17, 2014 at 04:35 UTC
    code would be nice ... as would a definition for  32 bit little endian value., integer, float, boat?

    I guess 73316 is an integer ... signed? unsigned?

    I remember, see Re: ID3v2 TAG Footer Reading goes wrong (more subs)

    sub Int32 { unpack 'l<', $_[-1] } sub UInt32 { unpack 'L<', $_[-1] } __END__ my $hex = pack 'H*', '641e0100'; dd( $hex ); dd( unpack 'i', $hex ); dd( unpack 'I', $hex ); dd( unpack 'j', $hex ); dd( unpack 'J', $hex ); dd( unpack 'l<', $hex ); dd( unpack 'L<', $hex );
Re: hex to binary
by LanX (Saint) on Feb 17, 2014 at 04:36 UTC
    Which patterns did you try with unpack and what did you get?

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Re: hex to binary
by Anonymous Monk on Feb 17, 2014 at 04:52 UTC
    it looks like this is what i'm after.
    my $hex = pack 'H*', '641e0100'; print unpack 'I', $hex;print "\n";

    Thanks for your help!

      you're looking for UInt32 because thats 32bit little endian on every platform :) as pack says The integer formats s, S , i , I , l , L , j , and J are inherently non-portable between processors and operating systems because they obey native byteorder and endianness.
Re: hex to binary
by Anonymous Monk on Feb 17, 2014 at 04:43 UTC
    Ah, my title is someone incorrect, Hex to Decimal is what it's meant to read.

    the 73316 is a decimal representation of the length of the file, so its an integer