int('\x23') != 0x23 (a.k.a convert char to integer of its byte representation)

Discussion in 'Python' started by =?utf-8?B?Qm9yaXMgRHXFoWVr?=, Sep 15, 2007.

  1. Hi,

    I am looking for the best way to convert a string of length 1 (= 1
    character as string) to integer that has the same value as numeric
    representation of that character. Background: I am writing functions
    abstracting endianness, e.g. converting a string of length 4 to the
    appropriate integer value (e.g. '\x01\x00\x00\x00' = 2**24 for big
    endian memory, 2**0 for little endian memory). For this, I need to
    know the numeric value of each byte and sum them according to
    endianness.

    I thought that something like int('\x01') might work, provided the
    argument is string of length 1, but that throws an error:

    >>> int('\x12')

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    ValueError: invalid literal for int():

    The code I want to write looks like this:

    mem = '\x11\x22\x33\x44'
    factor = 1
    sum = 0
    for byte in mem:
    sum += int(byte) * factor
    factor *= 2**8

    Could you please tell me how to achieve what I want in Python? (it
    would be straightforward in C)

    Thanks for any suggestions,
    Boris Dušek
     
    =?utf-8?B?Qm9yaXMgRHXFoWVr?=, Sep 15, 2007
    #1
    1. Advertising

  2. Re: int('\x23') != 0x23 (a.k.a convert char to integer of its byterepresentation)

    On Sat, 15 Sep 2007 11:55:28 +0000, Boris Dušek wrote:

    > I am looking for the best way to convert a string of length 1 (= 1
    > character as string) to integer that has the same value as numeric
    > representation of that character. Background: I am writing functions
    > abstracting endianness, e.g. converting a string of length 4 to the
    > appropriate integer value (e.g. '\x01\x00\x00\x00' = 2**24 for big
    > endian memory, 2**0 for little endian memory). For this, I need to
    > know the numeric value of each byte and sum them according to
    > endianness.


    So you are looking for the `struct` module in the standard library instead
    of doing this yourself. :)

    If you insist on doing it yourself take a look at the built-in `ord()`
    function.

    Ciao,
    Marc 'BlackJack' Rintsch
     
    Marc 'BlackJack' Rintsch, Sep 15, 2007
    #2
    1. Advertising

  3. On Sep 15, 1:59 pm, Marc 'BlackJack' Rintsch <> wrote:
    > So you are looking for the `struct` module in the standard library instead
    > of doing this yourself. :)
    >
    > If you insist on doing it yourself take a look at the built-in `ord()`
    > function.
    >


    Thanks Marc,

    both things are exactly what I was looking for (I am a bit ashamed
    that I did not find the built-in ord :)

    Boris
     
    =?utf-8?B?Qm9yaXMgRHXFoWVr?=, Sep 15, 2007
    #3
  4. =?utf-8?B?Qm9yaXMgRHXFoWVr?=

    John Machin Guest

    On Sep 15, 9:55 pm, Boris Dušek <> wrote:
    > Hi,
    >
    > I am looking for the best way to convert a string of length 1 (= 1
    > character as string) to integer that has the same value as numeric
    > representation of that character. Background: I am writing functions
    > abstracting endianness, e.g. converting a string of length 4 to the
    > appropriate integer value (e.g. '\x01\x00\x00\x00' = 2**24 for big
    > endian memory, 2**0 for little endian memory). For this, I need to
    > know the numeric value of each byte and sum them according to
    > endianness.
    >
    > I thought that something like int('\x01') might work, provided the
    > argument is string of length 1, but that throws an error:
    >
    > >>> int('\x12')

    >
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > ValueError: invalid literal for int():
    >
    > The code I want to write looks like this:
    >
    > mem = '\x11\x22\x33\x44'
    > factor = 1
    > sum = 0
    > for byte in mem:
    > sum += int(byte) * factor
    > factor *= 2**8
    >
    > Could you please tell me how to achieve what I want in Python? (it
    > would be straightforward in C)
    >


    'BlackJack' has already sent you looking for the docs for ord() and
    the struct module but a few other clues seem necessary:

    1. Don't "think that something like int() might work", read the docs.

    2. "factor *= 2 **8"?? Python compiles to bytecode; don't make it work
    any harder than it has to. "factor" is quite unnecessary. That loop
    needs exactly 1 statement:
    sum = (sum << 8) + ord(byte)

    3. Don't use "sum" as a variable name; it will shadow the sum() built-
    in function.

    4. Read through the docs for *all* the built-in
    functions -- all kinds of interesting and useful gadgets to be found
    there.

    5. In a dark corner there lives a strange beast called the array
    module:

    >>> import array
    >>> array.array('I', '\x01\x00\x00\x00')[0]

    1L
    >>> array.array('I', '\x00\x00\x00\x01')[0]

    16777216L
    >>> 2**24

    16777216
    >>>


    HTH,
    John
     
    John Machin, Sep 15, 2007
    #4
    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. Richard
    Replies:
    11
    Views:
    1,088
    Adam Maass
    Feb 1, 2006
  2. Schnoffos
    Replies:
    2
    Views:
    1,227
    Martien Verbruggen
    Jun 27, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,657
    Old Wolf
    Jan 20, 2004
  4. Philipp H. Mohr

    convert char to byte representation

    Philipp H. Mohr, Oct 10, 2005, in forum: Python
    Replies:
    7
    Views:
    13,745
    Mike Meyer
    Oct 11, 2005
  5. Polaris431
    Replies:
    8
    Views:
    762
    SM Ryan
    Dec 4, 2006
Loading...

Share This Page