You are not logged in.

#1 2012-10-31 03:33:39

moogtrain
Member
Registered: 2012-07-23
Posts: 10

[SOLVED] Reading binary data on Python 3.3

Why is is that in Python 3.3 (Arch x64), the file handler processes bytes in a weird order? Is this a bug or there's something else I do not know?

$ python
Python 3.3.0 (default, Sep 29 2012, 15:50:43) 
[GCC 4.7.1 20120721 (prerelease)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> fo = open("filetest", "w")
>>> fo.write("\x00")
1
>>> fo.write("\x01")
1
>>> fo.write("\x02")
1
>>> fo.write("\x03")
1
>>> fo.close()
>>> fo = open ("filetest", "rb")
>>> bin = fo.read(4)
>>> print(bin)
b'\x00\x01\x02\x03'

But...

$ hexdump filetest
0000000 0100 0302                              
0000004

Last edited by moogtrain (2012-10-31 06:50:16)

Offline

#2 2012-10-31 06:35:23

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 216

Re: [SOLVED] Reading binary data on Python 3.3

I believe hexdump is reading in and displaying the file as a sequence of 2-byte integers; least significant byte first. Try doing:

hexdump -C filetest

Offline

#3 2012-10-31 06:48:21

moogtrain
Member
Registered: 2012-07-23
Posts: 10

Re: [SOLVED] Reading binary data on Python 3.3

Oh, you were right. Thanks. big_smile

Offline

Board footer

Powered by FluxBB