Question

Hi I have problem with parsing -0.000000e+00 on linux( on windows is working).

struct.pack( "d", -0.000000e+00 )

On linux struct.pack change -0.000000e+00 to 0.000000e+00. When i print value before pack is correct but result of struct.pack is like it was 0.000000e+00.

Is there any solution to solve this problem.

I think i need to add negative number witch is closest to 0. How to do that?

EDIT struct.pack( "d", -0.000000e+00 ) result '\x00\x00\x00\x00\x00\x00\x00\x80'

struct.pack( "!d", -0.000000e+00 )result '\x00\x00\x00\x00\x00\x00\x00\x00'

struct.pack( "<d", -0.000000e+00 )result '\x00\x00\x00\x00\x00\x00\x00\x00'

struct.pack( ">d", -0.000000e+00 )result '\x00\x00\x00\x00\x00\x00\x00\x00' I want to use "< d" and " > d".

EDIT Sry not error.

Était-ce utile?

La solution

The struct format string "d" encodes the value in a platform-specific way. Most likely, the platform you decode the bytestring on has a different endianess or length of doubles. Use the ! format character to force a platform-independent encoding:

>>> struct.pack('!d', -0.)
b'\x80\x00\x00\x00\x00\x00\x00\x00' # IEEE754 binary64 Big Endian
>>> struct.unpack('!d', b'\x80\x00\x00\x00\x00\x00\x00\x00')[0]
-0.0

Also make sure that you use a supported Python version. In cPython<2.5, struct is known to be buggy. Update to a current version, like 2.7 or 3.2.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top