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
Answer from Peter DeGlopper on Stack Overflow
Discussions

Convert bytes to int and back java/python
How can I achieve same with java? You should probably post this in a Java-related subreddit. More on reddit.com
🌐 r/learnpython
2
1
May 7, 2019
Converting string of bytes to integer
I am working on interfacing Arduino with Julia. There is a particular experiment of reading the values from a sensor connected to Arduino. For this, I have written a function in Julia 1.6.0. This function reads the values coming from the serial port. These values are strings like @\x02. More on discourse.julialang.org
🌐 discourse.julialang.org
6
0
April 10, 2021
type conversion - Converting from byte to int in Java - Stack Overflow
Stack Internal Implement a knowledge platform layer to power your enterprise and AI tools. More on stackoverflow.com
🌐 stackoverflow.com
bytes -> int
You have to choose the right sizes for the data types and pad the slices with zero bytes: bytes1 := []byte{84, 48, 92, 0} // 4 bytes = 32 bits bytes2 := []byte{84, 48, 92, 91, 244, 0, 0, 0} // 8 bytes = 64 bits num1 := int(binary.LittleEndian.Uint32(bytes1)) fmt.Println("num1:", num1) num2 := int(binary.LittleEndian.Uint64(bytes2)) fmt.Println("num2:", num2) More on reddit.com
🌐 r/golang
2
8
July 6, 2023
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-convert-bytes-to-int-in-python
How to Convert Bytes to Int in Python?
March 27, 2026 - # Simulate reading 4-byte integers ... integers.append(integer) print(f"Converted integers: {integers}") ... The int.from_bytes() method is the most straightforward way to convert bytes to integers in Python....
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › csharp › programming-guide › types › how-to-convert-a-byte-array-to-an-int
How to convert a byte array to an int - C# | Microsoft Learn
This example initializes an array of bytes, reverses the array if the computer architecture is little-endian (that is, the least significant byte is stored first), and then calls the ToInt32(Byte[], Int32) method to convert four bytes in the ...
🌐
Delft Stack
delftstack.com › home › howto › python › how to convert bytes to integers
How to Convert Bytes to Int in Python 2.7 and 3.x | Delft Stack
February 2, 2024 - struct.unpack method in Python 2.7/Python 3.x and int.from_bytes() method in Python 3.x could convert bytes to integers.
🌐
Mkyong
mkyong.com › home › java › java – convert byte[] to int and vice versa
Java – Convert byte[] to int and vice versa | mkyong.com
May 10, 2020 - package com.mkyong.nio; import java.nio.ByteBuffer; public class ByteArrayToIntExample { public static void main(String[] args) { // byte = -128 to 127 byte[] byteArray = new byte[] {00, 00, 00, 01}; int result = convertByteArrayToInt2(byteArray); System.out.println("Byte Array (Hex) : " + convertBytesToHex(byteArray)); System.out.println("Result : " + result); } // method 1 public static int convertByteArrayToInt(byte[] bytes) { return ByteBuffer.wrap(bytes).getInt(); } // method 2, bitwise again, 0xff for sign extension public static int convertByteArrayToInt2(byte[] bytes) { return ((bytes[
Find elsewhere
🌐
GitHub
gist.github.com › d81604135a1b94b773b6
How to convert bytes to integer in python · GitHub
How to convert bytes to integer in python. GitHub Gist: instantly share code, notes, and snippets.
🌐
Coderwall
coderwall.com › p › x6xtxq › convert-bytes-to-int-or-int-to-bytes-in-python
Convert bytes to int or int to bytes in python (Example)
September 29, 2021 - 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
🌐
Bacancy Technology
bacancytechnology.com › qanda › golang › how-to-convert-from-byte-to-int-in-go
How to convert from []byte to int in Go Programming
September 15, 2023 - One of the simplest and most common ways to convert a []byte to an int is by using the strconv package in Go.
🌐
Baeldung
baeldung.com › home › java › java numbers › convert byte to int type in java
Convert byte to int Type in Java | Baeldung
July 6, 2024 - The above code example takes a byte as an input and will return an Integer instance of the specified byte value. The Java Compiler will automatically apply the unboxing since the Integer class serves as a wrapper for the primitive data type int. We can perform a test to verify its expected behavior:
🌐
Reddit
reddit.com › r/learnpython › convert bytes to int and back java/python
r/learnpython on Reddit: Convert bytes to int and back java/python
May 7, 2019 -

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

🌐
Julia Programming Language
discourse.julialang.org › general usage
Converting string of bytes to integer - General Usage - Julia Programming Language
April 10, 2021 - I am working on interfacing Arduino with Julia. There is a particular experiment of reading the values from a sensor connected to Arduino. For this, I have written a function in Julia 1.6.0. This function reads the valu…
🌐
Stack Overflow
stackoverflow.com › questions › 9581530 › converting-from-byte-to-int-in-java
type conversion - Converting from byte to int in Java - Stack Overflow
I have generated a secure random number, and put its value into a byte. Here is my code. SecureRandom ranGen = new SecureRandom(); byte[] rno = new byte[4]; ranGen.nextBytes(rno); int i = rno[0].
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to convert python bytes to int
5 Best Ways to Convert Python Bytes to Int - Be on the Right Side of Change
February 23, 2024 - The struct.unpack() function is called with a format string ‘>H‘ indicating a big-endian unsigned short and the bytes object. The resulting tuple’s first element contains the integer. Combine Python’s list comprehension with the reduce() function to convert bytes to an integer.
🌐
Tutorial Reference
tutorialreference.com › python › examples › faq › python-how-to-convert-bytes-to-int
How to Convert Bytes to Int in Python | Tutorial Reference
A bytes object in Python represents a sequence of raw binary data. Converting it to an integer means interpreting that binary sequence as a numeric value.
🌐
Reddit
reddit.com › r/golang › bytes -> int
r/golang on Reddit: bytes -> int
July 6, 2023 -

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 ?

🌐
SCADACore
scadacore.com › home › tools › programming calculators › online hex converter
Online Hex Converter - Bytes, Ints, Floats, Significance, Endians - SCADACore
April 8, 2021 - Hex-To-UINT (Unsigned Integer) and Hex-To-INT (Singed Integer) Converts the Hex string to the 4 different Endian Combinations. We also perform to 16 bit conversions, Hex-To-UINT16 (16 bit Unsigned Integer) and Hex-To-INT16 (16 bit Signed Integer) When interfacing with new hardware, it is sometimes difficult to determine the number format of a string of raw binary data. For industrial programmers and field technicians, looking at the communication data in byte format would show an array of bytes that could be difficult to translate into readable text or values.
Top answer
1 of 5
46

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.

2 of 5
20

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.

🌐
Bobby Hadz
bobbyhadz.com › blog › convert-int-to-bytes-in-python
How to convert Int to Bytes and Bytes to Int in Python | bobbyhadz
If signed is False and a negative integer is supplied, an OverflowError is raised. By default, the signed argument is set to False. The following function can be used if you need to convert signed bytes to integers.