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(nums tring):
"""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 2 4872
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(nums tring):
"""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>
--
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: jova |
last post by:
Does anyone know of a good site where I can learn of this?
|
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 THROWN JavaMailUtil.getMessage():Can't get the message :
java.io.UnsupportedEncodingException: BIG-5
at sun.io.Converters.getConverterClass(Converters.java:215)
|
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 "i" and two
"s". The function should also handle if String A and B have huge
values, like two big dictionary.
What's the best approach to achieve this with the best performance?
what's the Big O then?
I am thinking to put A and B into two...
|
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. Yahoo store was originally written in Lisp.
c. Emacs
The issues with these will probably come up, so I might as well
mention them myself (which will also make this a more balanced
|
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 size is too small for
what I want to do. So, how can I store numbers bigger than this one in
C?
Thanks, Guilherme
| |
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. After addding another
Crystal report, vs.net will no longer build the project - it just goes
away - no message no nothing. My other dev box will build it but won't run
it in debug.
I ran a vs.net repair but it still does the same thing. vs.net...
|
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 macro, and I ran a
command that told the application to process windows messages.
Is there such a thing in vb.net, or is there a way to make sure windows
messages get processed in big cpu greedy loops?
Also, Is it a big deal to make code multy...
|
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 imfo
would be transferred to the top of that page. However the users name
will only appear on a seperate page.
The code i am using (in brief) is as follows:
function getname()
{
|
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 memory ?
Thanks for any help
Pascal
|
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 be able to
represent the number (95 ^ 63), which is approximately (2 ^ 6.6) ^ 63,
which is about 2 ^ 416, so we're talking about maybe a 512-Bit number.
So I need to be able to do division and multiplication on a 512-Bit
number. Has anyone here...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |