ascii2dec

Discussion in 'Python' started by Uwe Mayer, Aug 20, 2003.

  1. Uwe Mayer

    Uwe Mayer Guest

    Hi,

    this must sound stupid to you, but I'm ages out of Python and I just can't
    find a function to convert 4 bytes of binary data to an integer value:

    length=f.read(4) # get length in binary
    length=socket.htonl(length) # swap bytes

    #convert 4 bytes to integer

    f.close()

    Thanks for any help
    Uwe
    --
     
    Uwe Mayer, Aug 20, 2003
    #1
    1. Advertising

  2. Uwe Mayer

    Uwe Mayer Guest

    Uwe Mayer wrote:

    > this must sound stupid to you, but I'm ages out of Python and I just can't
    > find a function to convert 4 bytes of binary data to an integer value:
    >
    > length=f.read(4) # get length in binary
    > length=socket.htonl(length) # swap bytes
    >
    > #convert 4 bytes to integer


    Sorry, socket.htonl(...) already expects a number, so its:

    > #convert 4 bytes to integer
    > length=socket.htonl(length) # swap bytes


    i.e.
    How to convert:

    length = '\x01\x00\x00\x00'

    to an integer

    Uwe
    --
     
    Uwe Mayer, Aug 20, 2003
    #2
    1. Advertising

  3. Uwe Mayer wrote:

    > this must sound stupid to you, but I'm ages out of Python and I just can't
    > find a function to convert 4 bytes of binary data to an integer value:
    >
    > length=f.read(4) # get length in binary
    > length=socket.htonl(length) # swap bytes
    >
    > #convert 4 bytes to integer
    >
    > f.close()
    >
    > Thanks for any help


    import struct
    result = struct.unpack("!i", f.read(4))
    length = result[0]

    where "!" means network byte order, and "i" means 32-bit integer.

    see the struct documentation for more options.

    if the f.read fails to read 4 bytes, this operation raises a struct.error
    exception

    note that unpack returns a tuple; you may prefer to unpack a bunch
    of fields in one step:

    width, height = struct.unpack("!ii", f.read(8))

    </F>
     
    Fredrik Lundh, Aug 20, 2003
    #3
  4. Uwe Mayer

    Peter Hansen Guest

    Uwe Mayer wrote:
    >
    > Uwe Mayer wrote:
    >
    > > this must sound stupid to you, but I'm ages out of Python and I just can't
    > > find a function to convert 4 bytes of binary data to an integer value:
    > >
    > > length=f.read(4) # get length in binary
    > > length=socket.htonl(length) # swap bytes
    > >
    > > #convert 4 bytes to integer

    >
    > Sorry, socket.htonl(...) already expects a number, so its:
    >
    > > #convert 4 bytes to integer
    > > length=socket.htonl(length) # swap bytes

    >
    > i.e.
    > How to convert:
    >
    > length = '\x01\x00\x00\x00'
    >
    > to an integer


    Use the struct module, with appropriate "endianism".

    -Peter
     
    Peter Hansen, Aug 20, 2003
    #4
  5. Uwe Mayer

    Sean Ross Guest

    "Uwe Mayer" <> wrote in message
    news:bi0c3p$8qh$-karlsruhe.de...
    > Uwe Mayer wrote:
    > How to convert:
    >
    > length = '\x01\x00\x00\x00'
    >
    > to an integer



    As Peter suggested, use struct.unpack with the appropriate endianness, e.g.

    >>> import struct
    >>> length = '\x01\x00\x00\x00'
    >>> # convert 'length' to signed integer (big-endian)
    >>> unpacked = struct.unpack('>i', length)[0] # unpack returns a tuple
    >>> unpacked

    16777216
    >>> # convert 'length' to signed integer (little-endian)
    >>> unpacked = struct.unpack('i', length)[0] # or, fmt = '<i'
    >>> unpacked

    1

    HTH
    Sean
     
    Sean Ross, Aug 21, 2003
    #5
    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.

Share This Page