473,395 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

finding byte order

Hello,

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.
I am using the struct module to read the data and it is fine because I
can specify in the format if the data are to be read with big or small
endian convention.

I would like to use the array module instead of struct because it is
suppose to be faster for big arrays. However, this module does not
provide a format specifier to say if the data are writtent with big or
small endian. The result is that it works on big-endian machine and
not on small-endian machine.

I would like to have a test to tell me if the current machine is
using big or small endian, this way I could use the array module in
the first case and the *slower* struct module on the second. I looked
but did not find. Is there a python function to know that?

Thanks!
Jul 18 '05 #1
6 3531
biner wrote:
I would like to have a test to tell me if the current machine is
using big or small endian, this way I could use the array module in
the first case and the *slower* struct module on the second. I looked
but did not find. Is there a python function to know that?

Thanks!


How about sys.byteorder?

At least if you are using array.array, note the "byteswap" method:
import array
v = array.array('h',range(8))
v array('h', [0, 1, 2, 3, 4, 5, 6, 7]) v.byteswap()
v

array('h', [0, 256, 512, 768, 1024, 1280, 1536, 1792])

--Scott David Daniels
Sc***********@Acm.Org
Jul 18 '05 #2
> 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.

--
Regards,

Diez B. Roggisch
Jul 18 '05 #3
> I would like to have a test to tell me if the current machine is
using big or small endian, this way I could use the array module in
the first case and the *slower* struct module on the second. I looked
but did not find. Is there a python function to know that?


There is no such test, as it's domain-specific if there _can_ be such a test
or not. If your data is composed in a way that you can infer the endianess
by reading e.g. a header at the beginning that has well-known fields, then
it might work. But only you can do that.

For a pure binary file, there is absolutely no way of telling the endianess.
--
Regards,

Diez B. Roggisch
Jul 18 '05 #4
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
Sc***********@Acm.Org
Jul 18 '05 #5
> 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.


I'm sorry, I confused that he asked for machine endianess. Then of course
you are right.

--
Regards,

Diez B. Roggisch
Jul 18 '05 #6
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
Sc***********@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!

Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: jeff | last post by:
how do you convert form byte to Int32 while retaining the binary value of the byte array
3
by: Helgardh | last post by:
I have a linked table (Access 2003) to an Outlook inbox. The body of the e-mail messages are in a memo field. My problem is that I need to "read" the memo and find data on certain lines. The...
2
by: Jesse Engle | last post by:
i'm learning how to do some basic low-level network programming. the site i'm reading talks about "network byte order" and "host byte order". the thing is, it doesn't give an explanation as to what...
12
by: Michi Henning | last post by:
Looking at the language spec, I can't find a statement about the byte order for value types, such as int, float, etc. Are they guaranteed to be little-endian or big- endian? I know that, on a...
6
by: Ricardo Quintanilla | last post by:
i have a code that sends data to a socket listening over as400 platform, the socket responds to me as a "byte array". then i need to convert the "byte array" into a string. the problem is that...
6
by: lovecreatesbeauty | last post by:
/* It seems that when an int with width of four bytes is assigned to a one byte width char, the first three bytes from left to right are discarded and the rightest byte is assigned to that char....
33
by: Benjamin M. Stocks | last post by:
Hello all, I've heard differing opinions on this and would like a definitive answer on this once and for all. If I have an array of 4 1-byte values where index 0 is the least signficant byte of a...
4
by: Frederick Gotham | last post by:
What do you think of the following code for setting and retrieving the value of bytes in an unsigned integer? The least significant bit has index 0, then the next least significant bit has index 1,...
11
by: nephish | last post by:
hello there, all. i have a difficult app that connects to a server to get information for our database here. this server is our access point to some equipment in the field that we monitor. ...
5
by: moni | last post by:
Hey, My buffer contains a short int, some char, and a structure in form of a byte array. Read the string as: TextBox4.Text = System.Text.Encoding.ASCII.GetString(buffer1, 0, 31); Read...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.