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


in reply to pack and unpack trouble

$mCrc = $mPolyTable[ ( $mCrc ^ $iByteArray[$x] ) & 0xFF ];

The closest pack equivalent to a (byte)val would be

unpack('C', substr(pack('J', $val), 0, 1))

or

unpack('C', substr(pack('J', $val), -1, 1))

depending on your machine. That could be made portable as

unpack('C', substr(pack('J<', $val), 0, 1))

which simplifies to

unpack('C', pack('J<', $val))

But what a waste.

Update: Added the content underneath the bar.

Replies are listed 'Best First'.
Re^2: pack and unpack trouble
by scoobyrico (Beadle) on Dec 22, 2011 at 18:30 UTC
    Excellent! as a follow up, if I want to take 1365238130 to 515FE172? Upate: I would have thought it was hex(1365238130) but that gets me 83301204272. So clearly I am doing something wrong.
    In C#
    uint temp = 1365238130; System.Console.WriteLine(temp.ToString("X"));
      Adapted from perlfaq4: How do I convert between numeric representations/bases/radixes?
      my $hex_string = sprintf '%X', 1365238130;

      hex converts *from* hex to a number.

      sprintf '%X', $n will represent a number using hex.

        Thanks so much! I had totally forgotten about sprintf