finding byte order

biner.sebastien at ouranos.ca biner.sebastien at ouranos.ca
Tue Dec 7 14:30:37 EST 2004


Scott David Daniels wrote:
> biner wrote:
>  >   I am using a program that has to read binary data from files
coming
>  > from different machines. The file are always written with big
endian.
>
> Diez B. Roggisch wrote:
>   [Scott David Daniels wrote]
> >>How about sys.byteorder?
> > This doesn't help, as he wants to read files from varying endianess
- what
> > the _current_ endianess is doesn't matter here.
>
> But, in fact, he says the files are always big endian.  So, code like
> the following should address his problem.  Note I use type 'h' as an
> example so I can easily read samples.
>
>      import sys, array
>      f =open('huge.dat')
>      v = array.array('h')   # Or whatever data type
>      v.fromfile(f, 4096)
>      f.close()
>      if sys.byteorder == 'little':
>          v.byteswap()
>
> --Scott David Daniels
> Scott.Daniels at Acm.Org

This seems to do the what I want. I did not know about array.byteswap
and sys.byteorder.
Thanks for taking the time to answer my silly question.

Ciao!




More information about the Python-list mailing list