Python bytes like object to int

I'm currently working on an encryption/decryption program and I need to be able to convert bytes to an integer. I know that:

bytes([3]) = b'\x03'

Yet I cannot find out how to do the inverse. What am I doing terribly wrong?

ggorlen

36.3k7 gold badges61 silver badges74 bronze badges

asked Nov 30, 2015 at 23:04

4

Assuming you're on at least 3.2, there's a built in for this:

int.from_bytes( bytes, byteorder, *, signed=False )

...

The argument bytes must either be a bytes-like object or an iterable producing bytes.

The byteorder argument determines the byte order used to represent the integer. If byteorder is "big", the most significant byte is at the beginning of the byte array. If byteorder is "little", the most significant byte is at the end of the byte array. To request the native byte order of the host system, use sys.byteorder as the byte order value.

The signed argument indicates whether two’s complement is used to represent the integer.

## Examples:
int.from_bytes(b'\x00\x01', "big")                         # 1
int.from_bytes(b'\x00\x01', "little")                      # 256

int.from_bytes(b'\x00\x10', byteorder='little')            # 4096
int.from_bytes(b'\xfc\x00', byteorder='big', signed=True)  #-1024

Python bytes like object to int

martineau

115k25 gold badges160 silver badges282 bronze badges

answered Nov 30, 2015 at 23:08

Peter DeGlopperPeter DeGlopper

35.2k7 gold badges86 silver badges81 bronze badges

4

Lists of bytes are subscriptable (at least in Python 3.6). This way you can retrieve the decimal value of each byte individually.

>>> intlist = [64, 4, 26, 163, 255]
>>> bytelist = bytes(intlist)       # b'@\x04\x1a\xa3\xff'

>>> for b in bytelist:
...    print(b)                     # 64  4  26  163  255

>>> [b for b in bytelist]           # [64, 4, 26, 163, 255]

>>> bytelist[2]                     # 26 

Python bytes like object to int

answered May 7, 2020 at 17:16

Python bytes like object to int

0

int.from_bytes( bytes, byteorder, *, signed=False )

doesn't work with me I used function from this website, it works well

https://coderwall.com/p/x6xtxq/convert-bytes-to-int-or-int-to-bytes-in-python

def bytes_to_int(bytes):
    result = 0
    for b in bytes:
        result = result * 256 + int(b)
    return result

def int_to_bytes(value, length):
    result = []
    for i in range(0, length):
        result.append(value >> (i * 8) & 0xff)
    result.reverse()
    return result

answered Nov 9, 2018 at 21:12

Python bytes like object to int

1

In case of working with buffered data I found this useful:

int.from_bytes([buf[0],buf[1],buf[2],buf[3]], "big")

Assuming that all elements in buf are 8-bit long.

answered Jun 1, 2021 at 8:23

list() can be used to convert bytes to int (works in Python 3.7):

list(b'\x03\x04\x05')
[3, 4, 5]

answered Apr 22 at 7:58

2

An old question that I stumbled upon while looking for an existing solution. Rolled my own and thought I'd share because it allows you to create a 32-bit integer from a list of bytes, specifying an offset.

def bytes_to_int(bList, offset):
    r = 0
    for i in range(4):
        d = 32 - ((i + 1) * 8)
        r += bList[offset + i] << d
    return r

answered May 19 at 14:12

Python bytes like object to int

Not the answer you're looking for? Browse other questions tagged python python-3.x int type-conversion byte or ask your own question.

What is bytes like object in Python?

Bytes-like object in python In Python, a string object is a series of characters that make a string. In the same manner, a byte object is a sequence of bits/bytes that represent data. Strings are human-readable while bytes are computer-readable. Data is converted into byte form before it is stored on a computer.

What does byte () do in Python?

Python bytes() Function The bytes() function returns a bytes object. It can convert objects into bytes objects, or create empty bytes object of the specified size.

Can we assign byte to int?

We can directly assign the byte to the int data type. Secondly, we have a Wrapper class method intValue() that returns the value of byte as an int after widening the primitive conversion as we're storing a smaller data type into a larger one. If we take the byte as unsigned, then we have the Byte.

How are bytes represented in Python?

In Python, a byte string is represented by a b , followed by the byte string's ASCII representation. A byte string can be decoded back into a character string, if you know the encoding that was used to encode it.