473,395 Members | 1,937 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.

Big-endian binary data to/from Python ints?

Here are a couple of functions that I feel stupid for having written.
They work, and they're pretty straightforward; it's just that I feel like
I must be missing an easier way to do this...

def net_to_int(numstring):
"""Convert a big-endian binary number, in the form of a string of
arbitrary length, to a native int.
"""
num = 0
for i in numstring:
num *= 256
num += ord(i)
return num

def int_to_net(num):
"""Convert a native int to a four-byte big-endian number, in the form
of a string.
"""
numstring = ''
for i in xrange(4):
numstring = chr(num % 256) + numstring
num /= 256
return numstring

The situation: I'm getting a four-byte packet from a socket that consists
of a big-endian 32-bit integer. (It specifies the length of the data that
follows.) I have to send the same thing in reply. send() and recv() work
with strings... I'm familiar with ntohl() and htonl(), but those expect/
return integers.

--
09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 -- pass it on
Dec 26 '07 #1
2 4855
William McBrine wrote:
Here are a couple of functions that I feel stupid for having written.
They work, and they're pretty straightforward; it's just that I feel like
I must be missing an easier way to do this...

def net_to_int(numstring):
"""Convert a big-endian binary number, in the form of a string of
arbitrary length, to a native int.
"""
num = 0
for i in numstring:
num *= 256
num += ord(i)
return num

def int_to_net(num):
"""Convert a native int to a four-byte big-endian number, in the form
of a string.
"""
numstring = ''
for i in xrange(4):
numstring = chr(num % 256) + numstring
num /= 256
return numstring

The situation: I'm getting a four-byte packet from a socket that consists
of a big-endian 32-bit integer. (It specifies the length of the data that
follows.) I have to send the same thing in reply. send() and recv() work
with strings... I'm familiar with ntohl() and htonl(), but those expect/
return integers.
The struct module should do it, but do you prefer your code or format
strings? :-P

<http://docs.python.org/lib/module-struct.html>
--
Dec 26 '07 #2
On Wed, 26 Dec 2007 16:50:53 -0800, Dennis Lee Bieber wrote:
your code might (I've not actually checked it) be incorrect if ported
to another machine.
Nope. :-)
If the problem is that you have the four bytes as a character string,
use the struct module to interpret it as a binary integer and then feed
the results to the above functions, followed by using struct to convert
back to a string representation.

Heck, looking at struct, it already has a format modifier for
net-oriented... (assuming unsigned 32-bit integer)

pinteger = struct.unpack("!I", netstring[0:4])[0]
and
netstring = struct.pack("!I", pinteger)
Thanks, that seems to be what I was looking for.

--
09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 -- pass it on
Dec 27 '07 #3

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

Similar topics

6
by: jova | last post by:
Does anyone know of a good site where I can learn of this?
0
by: Kwok Ng | last post by:
When I make the following call with the mail has BIG-5 content type, Part messagePart = message; Object content = messagePart.getContent(); It throws the following exception EXCEPTION...
13
by: usgog | last post by:
I need to implement a function to return True/false whether String A contains String B. For example, String A = "This is a test"; String B = "is is". So it will return TRUE if String A includes two...
303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
39
by: hobbs | last post by:
Hi all I want to make a program that will need to use too long integers and the biggest size that I know is unsigned long, which stores numbers until 4294967295 (in my Turbo C 3.1), but this...
7
by: Tina | last post by:
I have an asp project that has 144 aspx/ascx pages, most with large code-behind files. Recently my dev box has been straining and taking long times to reneder the pages in the dev environment. ...
3
by: Fred Flintstone | last post by:
I have a project, that loops through a lot of wmi queries, and while its looping, the window will not repaint, I can't move or size the window. I remember having a similar problem back in an excel...
17
by: cass27 | last post by:
I am just doing a course on Javascript and have begun to try out different ideas. The effect I wanted was fro the user to click on a graphic. Aprompt box would apprear asking their name and that...
4
by: Pascal Sartoretti | last post by:
Hello, I want to process big files (e.g. 100 MB) with XQuery. Are there XQuery processors that work in a "SAX-like" way (and not DOM-like), i.e. which don't assume that they have everything in...
14
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
I need a Big Number library. I've been considering switching my project to C++ but at the moment I'm exploring the avenue of keeping it in C. What's the best Big Number library for C? I need to...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.