472,958 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 4839
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.