convert n-byte string into integer

M

Maligree

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.
 
M

MRAB

Maligree said:
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.
 
M

Maligree

Maligree said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top