Assuming you're on at least 3.2, there's a built in for this:
int.from_bytes(bytes,byteorder, *,signed=False)...
The argument
bytesmust either be a bytes-like object or an iterable producing bytes.The
byteorderargument determines the byte order used to represent the integer. Ifbyteorderis"big", the most significant byte is at the beginning of the byte array. Ifbyteorderis"little", the most significant byte is at the end of the byte array. To request the native byte order of the host system, usesys.byteorderas the byte order value.The
signedargument 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
Answer from Peter DeGlopper on Stack OverflowAssuming you're on at least 3.2, there's a built in for this:
int.from_bytes(bytes,byteorder, *,signed=False)...
The argument
bytesmust either be a bytes-like object or an iterable producing bytes.The
byteorderargument determines the byte order used to represent the integer. Ifbyteorderis"big", the most significant byte is at the beginning of the byte array. Ifbyteorderis"little", the most significant byte is at the end of the byte array. To request the native byte order of the host system, usesys.byteorderas the byte order value.The
signedargument 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
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
Convert bytes to int and back java/python
Converting string of bytes to integer
type conversion - Converting from byte to int in Java - Stack Overflow
bytes -> int
I am converting an arbitrary ascii string, which is incidently a number padded with zeros, for example "1" as "0000000001" then to bytes then back in python. Of course the value can be "0000004231" etc. also. It is always numeric and within range of signed 32bit value padded by zeros.
When I tell python that it is bytes and I want it in int, it converts it to a nice large random looking number. Then I can convert it back to original value later using to_bytes() function.
In [74]: value = int.from_bytes(bytes(format(1, '010d'),'ascii'), byteorder='little') In [75]: value.to_bytes(10,byteorder=sys.byteorder) Out[75]: b'0000000001' In [76]: value Out[76]: 232284873704446901628976 In [77]:
How can I achieve same with java?
Note: I need the number to be padded with zeros and 10 characters long. It is a number in range of 32bit signed int with padding to fixed 10 character length
Note 2 : I already tried this. I get [B@7852e922 in the testBytesvariable and not 232284873704446901628976 which I expect
Hi,I am trying to rewrite some python lines to golang and I am stuck on converting byte array to int (little-endian byteorder)
python:
bytes1 = [84, 48, 92]bytes2 = [84, 48, 92, 91, 244]num1 = int.from_bytes(bytes1, "little")print(f"num1: ", num1)num2 = int.from_bytes(bytes2, "little")print(f"num2: ", num2)
gives me output:
num1: 6041684
num2: 1049504788564
and in golang :
`bytes1 := []byte{84, 48, 92}`
`bytes2 := []byte{84, 48, 92, 91, 244}`
`num1 := int(binary.LittleEndian.Uint16(bytes1))`
`fmt.Println("num1:", num1)`
`num2 := int(binary.LittleEndian.Uint32(bytes2))`
`fmt.Println("num2:", num2)`
I get:
num1: 12372
num2: 1532768340
can someone tell me what am I doing wrong ?
There's no standard function to do it for you in C. You'll have to assemble the bytes back into your 16- and 32-bit integers yourself. Be careful about endianness!
Here's a simple little-endian example:
extern uint8_t *bytes;
uint32_t myInt1 = bytes[0] + (bytes[1] << 8) + (bytes[2] << 16) + (bytes[3] << 24);
For a big-endian system, it's just the opposite order:
uint32_t myInt1 = (bytes[0] << 24) + (bytes[1] << 16) + (bytes[2] << 8) + bytes[3];
You might be able to get away with:
uint32_t myInt1 = *(uint32_t *)bytes;
If you're careful about alignment issues.
Yes there is. Assume your bytes are in:
uint8_t bytes[N] = { /* whatever */ };
We know that, a 16 bit integer is just two 8 bit integers concatenated, i.e. one has a multiple of 256 or alternatively is shifted by 8:
uint16_t sixteen[N/2];
for (i = 0; i < N; i += 2)
sixteen[i/2] = bytes[i] | (uint16_t)bytes[i+1] << 8;
// assuming you have read your bytes little-endian
Similarly for 32 bits:
uint32_t thirty_two[N/4];
for (i = 0; i < N; i += 4)
thirty_two[i/4] = bytes[i] | (uint32_t)bytes[i+1] << 8
| (uint32_t)bytes[i+2] << 16 | (uint32_t)bytes[i+3] << 24;
// same assumption
If the bytes are read big-endian, of course you reverse the order:
bytes[i+1] | (uint16_t)bytes[i] << 8
and
bytes[i+3] | (uint32_t)bytes[i+2] << 8
| (uint32_t)bytes[i+1] << 16 | (uint32_t)bytes[i] << 24
Note that there's a difference between the endian-ness in the stored integer and the endian-ness of the running architecture. The endian-ness referred to in this answer is of the stored integer, i.e., the contents of bytes. The solutions are independent of the endian-ness of the running architecture since endian-ness is taken care of when shifting.