in reply to how to unpack a big-endian signed short integer

Instead of 's', use 'n'.

's' is unsigned short in your platform specific/c-compiler format.

'n' is unsigned short in "network" (big endian) format.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
Hooray!

  • Comment on Re: how to unpack a big-endian signed short integer

Replies are listed 'Best First'.
Re: Re: how to unpack a big-endian signed short integer
by robinbowes (Beadle) on Oct 15, 2003 at 19:52 UTC
    Thanks for the suggestion. However the values I'm reading are signed, i.e. signed, big-endian 16-bit integers. There doesn't seem to be a an appropriate format character for this. Bummer!

    -- Robin Bowes | http://robinbowes.com

      Sorry. I misread that. Try this...

      print unpack 's', pack 'S', unpack 'n', $n;

      Unpacking it with 'n' will handle the big-endianess without dropping bits. Repacking it with 'S' should likewise not drop anything, but will get it into a format that 's' will correctly unpack.

      I would use 'n' in the composite unpack and then

      sub utos16{ unpack 's', pack 'S', $_[0] }

      to retrieve the signedness afterwards.

      (Note: Please check I got that right:)


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      Hooray!