in reply to Re^3: -1 from hex ffffffffffffffff ?
in thread -1 from hex ffffffffffffffff ?

Representations of numbers are always big endian. The sole case to have little endian numbers I can remember encountering used giant red flags to avoid confusion since representation of numbers are always big endian.

Update: I suppose the first statement may not apply where non-latin scripts are involved. (I bet the most significant digit is placed first in all languages.)

Replies are listed 'Best First'.
Re^5: -1 from hex ffffffffffffffff ?
by almut (Canon) on Oct 02, 2009 at 19:04 UTC
    Representations of numbers are always big endian.

    The OP said hex string, not hex number.

    To elaborate, lets take the decimal number 1000.  Depending on whether it's stored big- or little-endian (in memory, in a file, etc.), you could find it as the (2-byte) string 03e8, or e803 (written in hex), and you could convert it to the number 1000 using any of the following (where 'n' and 'v' stand for "unsigned 16-bit in big-endian order" and "unsigned 16-bit in little-endian order" respectively):

    print unpack('n', pack('H*', '03e8')), "\n"; # 1000 print unpack('v', reverse pack('H*', '03e8')), "\n"; # 1000 print unpack('v', pack('H*', 'e803')), "\n"; # 1000 print unpack('n', reverse pack('H*', 'e803')), "\n"; # 1000

    Whereas the correct usage of 'n' or 'v' in combination with reverse does matter here, it doesn't when you have the symmetric hex string 'ffff'. Any combination will do (in particular, it's irrelevant whether you use the big- or little-endian unpack pattern):

    print unpack('n', pack('H*', 'ffff')), "\n"; # 65535 print unpack('v', reverse pack('H*', 'ffff')), "\n"; # 65535 print unpack('v', pack('H*', 'ffff')), "\n"; # 65535 print unpack('n', reverse pack('H*', 'ffff')), "\n"; # 65535 print unpack('n', reverse pack('H*', 'ffff')), "\n"; # 65535 print unpack('v', pack('H*', 'ffff')), "\n"; # 65535 print unpack('v', reverse pack('H*', 'ffff')), "\n"; # 65535 print unpack('n', pack('H*', 'ffff')), "\n"; # 65535

    (think of half of the 'ffff' as big-endian, and the other half as little-endian — as with 03e8/e803 above)

      Yes, 03E8 may be stored as bytes 03 E8 or as bytes E8 03, but it's silly to claim that 03E8 == E803. Hex is a numerical representation. You can't concatenate numbers together and claim you still have the same number.

      As you pointed out yourself, the OP said hex string. He said nothing of about a string of bytes and gave no indication that he was talking about a string of bytes (such as by separating them by spaces).

        You can't concatenate numbers together

        I think it's pretty common to write a hex string as a concatenated sequence of hex bytes — even Perl does it:

        $ perl -e"print unpack('H*', 'foobar')" 666f6f626172

        Think of that foobar as a sequence of bytes in memory representing a little-endian number...