convert n-byte string into integer

Discussion in 'Python' started by Maligree, Apr 28, 2010.

  1. Maligree

    Maligree Guest

    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.
    Maligree, Apr 28, 2010
    #1
    1. Advertising

  2. Maligree

    MRAB Guest

    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.
    MRAB, Apr 28, 2010
    #2
    1. Advertising

  3. Maligree

    Maligree Guest

    On Apr 28, 11:03 pm, MRAB <> 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.
    Maligree, Apr 28, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Spendius
    Replies:
    16
    Views:
    21,498
    Jon Skeet
    Sep 8, 2003
  2. Polaris431
    Replies:
    8
    Views:
    736
    SM Ryan
    Dec 4, 2006
  3. Replies:
    1
    Views:
    466
    Thomas J. Gritzan
    Jul 20, 2006
  4. Replies:
    4
    Views:
    1,344
  5. Godzilla
    Replies:
    15
    Views:
    492
    jigloo
    Jul 15, 2007
Loading...

Share This Page