473,386 Members | 1,715 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,386 software developers and data experts.

Endianness conversion

As part of a program I'm writing, I need to save to disk big amounts of
data (hundreds of MB, in 8kB chunks) swapping every couple of bytes.

I obviously cannot do it in a Python loop.

Is there a function I could use in the standard library, or do I have to
write my own C extension?
Toby
Feb 24 '07 #1
6 7722
On Sat, 24 Feb 2007 15:39:53 +0000, Toby wrote:
As part of a program I'm writing, I need to save to disk big amounts of
data (hundreds of MB, in 8kB chunks) swapping every couple of bytes.

I obviously cannot do it in a Python loop.

Is there a function I could use in the standard library, or do I have to
write my own C extension?
You could try the struct module. If your input comes in fixed sized
chunks, just call struct.unpack and struct.pack once per chunk.

HTH,
Dan

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
Atoms are not things. -- Werner Heisenberg.

Feb 24 '07 #2
Dan Sommers wrote:
You could try the struct module. If your input comes in fixed sized
chunks, just call struct.unpack and struct.pack once per chunk.
Thanks, but it was a bit awkward to use for big chunks.

I ended up writing my own byteswapper in Pyrex:
def swapbytes(data):
"Swap every two bytes of a even-sized python string, in place"
cdef int i
cdef char t, *p
p = data
for i from 0 <= i < len(data) / 2:
t = p[0]
p[0] = p[1]
p[1] = t
p = p + 2
Toby
Feb 24 '07 #3
On Feb 24, 9:39 am, Toby <etat...@gmail.comwrote:
As part of a program I'm writing, I need to save to disk big amounts of
data (hundreds of MB, in 8kB chunks) swapping every couple of bytes.

I obviously cannot do it in a Python loop.

Is there a function I could use in the standard library, or do I have to
write my own C extension?
Try the using the array module. array objects provide a byteswap
method which reverses endianness.

-Daniel

Feb 24 '07 #4
Daniel Harding wrote:
Try the using the array module. array objects provide a byteswap
method which reverses endianness.
Thanks!
Toby
Feb 24 '07 #5
En Sat, 24 Feb 2007 14:27:12 -0300, Toby <et*****@gmail.comescribió:
I ended up writing my own byteswapper in Pyrex:
You can use the byteswap method of arrays:
>>import array
a = array.array('H', 'ABcd56')
a.tostring()
'ABcd56'
>>a.byteswap()
a.tostring()
'BAdc65'

--
Gabriel Genellina

Feb 25 '07 #6
On Sat, 24 Feb 2007 17:27:12 +0000, Toby wrote:
Dan Sommers wrote:
>You could try the struct module. If your input comes in fixed sized
chunks, just call struct.unpack and struct.pack once per chunk.

Thanks, but it was a bit awkward to use for big chunks.
def swapper( bytestring ):
someHs = len( bytestring ) / 2 * "H" # could check for odd-lengths?
unpackfmt = "<" + someHs
packfmt = ">" + someHs
return struct.pack( packfmt, *struct.unpack( unpackfmt, bytestring )

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
Atoms are not things. -- Werner Heisenberg.

Feb 25 '07 #7

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

Similar topics

3
by: kelvSYC | last post by:
Are there any endianness concerns in C++, or does the compiler take care of those details? I ask because I'm not sure if code such as the following have consistent behavior on all platforms. ...
26
by: Case | last post by:
#include <string.h> int i; /* 4-byte == 4-char */ char data = { 0x78, 0x56, 0x34, 0x12 }; int main() { memcpy(&i, data, 4); /*
15
by: T Koster | last post by:
Hi group, I'm having some difficulty figuring out the most portable way to read 24 bits from a file. This is related to a Base-64 encoding. The file is opened in binary mode, and I'm using...
2
by: SSM | last post by:
Hi, Does C standard comment about "Endianness" to be used to store a structure/union variables? Thanks & Regards, Mehta
72
by: gamehack | last post by:
Hi all, I was thinking today, suppose we have the number n = 0xAB 0xFF which is equivalent to 44031 in decimal. In big endian it will be stored as 10101011 11111111 but in little endian...
18
by: friend.05 | last post by:
Code to check endianness of machine
6
by: Tomás | last post by:
Let's say you want to write fully portable code that will be writing files or sending data, and the data is text encoded using Unicode 16-Bit. Endianness comes into play. I'm writing code at the...
18
by: Indian.croesus | last post by:
Hi, If I am right Endianness is CPU related. I do not know if the question is right in itself but if it is then how does C handle issues arising out of Endianness. I understand that if we pass...
5
by: Rahul | last post by:
Hi Everyone, I have a program unit which does >and << of an integer which is of 4 bytes length. The logic of shifting and action based on the result, assumes that the system is big-endian. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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,...

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.