Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > convert n-byte string into integer

Reply
Thread Tools

convert n-byte string into integer

 
 
Maligree
Guest
Posts: n/a
 
      04-28-2010
Hello there.

Since this is one of those problems that may have some painfully
simple solution that will make me feel like a fool, I'll start of with
this: it's _very_ late (.. yeah, it's not).

To the point.

Data returned by socket.recvfrom is a string. Obviously one that most
of the time will contain non-printable characters. Let's say
data[10:14] is a four byte integer. How can I convert the string
data[10:14] to a python integer object?

Or perhaps there's a totally different, *Pythonic*, approach to this
(extracting data from these packet-representing strings). C may have
poisoned my mind.

Thanks for reading, hoping for some ideas.

 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      04-28-2010
Maligree wrote:
> Hello there.
>
> Since this is one of those problems that may have some painfully
> simple solution that will make me feel like a fool, I'll start of with
> this: it's _very_ late (.. yeah, it's not).
>
> To the point.
>
> Data returned by socket.recvfrom is a string. Obviously one that most
> of the time will contain non-printable characters. Let's say
> data[10:14] is a four byte integer. How can I convert the string
> data[10:14] to a python integer object?
>
> Or perhaps there's a totally different, *Pythonic*, approach to this
> (extracting data from these packet-representing strings). C may have
> poisoned my mind.
>
> Thanks for reading, hoping for some ideas.
>

Use the 'unpack' function from the 'struct' module.
 
Reply With Quote
 
 
 
 
Maligree
Guest
Posts: n/a
 
      04-28-2010
On Apr 28, 11:03*pm, MRAB <(E-Mail Removed)> wrote:
> Maligree wrote:
> > Hello there.

>
> > Since this is one of those problems that may have some painfully
> > simple solution that will make me feel like a fool, I'll start of with
> > this: it's _very_ late (.. yeah, it's not).

>
> > To the point.

>
> > Data returned by socket.recvfrom is a string. Obviously one that most
> > of the time will contain non-printable characters. Let's say
> > data[10:14] is a four byte integer. How can I convert the string
> > data[10:14] to a python integer object?

>
> > Or perhaps there's a totally different, *Pythonic*, approach to this
> > (extracting data from these packet-representing strings). C may have
> > poisoned my mind.

>
> > Thanks for reading, hoping for some ideas.

>
> Use the 'unpack' function from the 'struct' module.


Ah, thanks a lot.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Split string (then) Convert string into Integer news ASP General 2 05-26-2010 11:58 AM
Change a string to an integer, report an error if the string does not represent an integer? Randy Kramer Ruby 12 10-25-2007 09:56 PM
How to convert a string into an integer yinglcs@gmail.com Python 2 01-22-2007 10:02 PM
How to convert an integer into a string silverburgh.meryl@gmail.com C++ 8 02-01-2006 05:20 PM
Convert numeric String into Integer (Java) Azmie Java 12 12-30-2003 04:33 AM



Advertisments