1

I have been unpacking binary using python 2.7 on windows for months, but when I transferred to linux python 2.7 I get a value error.

I am using the standard struct.unpack_from function, with the option "L" for unsigned long structure. (https://docs.python.org/2/library/struct.html)

When I change the structure to "I" for unsigned integer I don't get the value error, but I'm wary of using this. The closest problem I could find similar to mine: Problems with python 2.4 and 2.4.4 in struct.unpack and win/lin

Is there a way to make sure I get the same implementation of data types between the windows and linux compilers?

EDIT: For future readers who have encountered a similar problem. If you are using long on windows and switch to Linux, the data types consist of a different number of bytes between the two operating systems. Long (unsigned) on windows is 4 bytes, but 8 on linux. Switching to unsigned int worked for me.

  • 2
    Can you be more specific about the Value error? – Scott Hunter Apr 11 '18 at 15:19
  • https://stackoverflow.com/questions/9689049/what-decides-the-sizeof-an-integer It might just be that they interpret the `sizeof(long)` differently between the compilers, which is fine. You can unpack accordingly by checking `os.name == 'nt'` for windows – Chrispresso Apr 11 '18 at 15:24
  • 2
    The [docs](https://docs.python.org/2/library/struct.html#byte-order-size-and-alignment) state that "By default, C types are represented in the machine’s native format and byte order" but "alternatively, the first character of the format string can be used to indicate the byte order, size and alignment of the packed data." So if you want a specific definition of a format you need to specify it. – Steven Rumbalski Apr 11 '18 at 15:27
  • The value error is from unpacking an epoch time stamp. It says the year is out of range. – user9591694 Apr 11 '18 at 15:51
  • 1
    Please reduce your failing code to the shortest possible reproducible test case. [Edit] your question and copy-paste that code along with any input, expected output and actual output. Copy-paste any error message, including full traceback. See [mcve] for more information. – Robᵩ Apr 11 '18 at 17:09
  • This is more of a response to data type differences between windows and linux. I am unpacking from binary, so I couldn't give you a test case. The function I am using is listed in the original post. – user9591694 Apr 11 '18 at 17:12

0 Answers0