How to convert a byte array to its numeric value Java?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Converting a byte array into a numeric value in Java is mostly a question of size and byte order. The code is simple once you know whether the bytes represent an int, long, unsigned value, or arbitrary-precision number, and whether they are encoded in big-endian or little-endian form.
Start with Endianness
Before writing any conversion code, decide the byte order. Big-endian means the most significant byte comes first. Little-endian means the least significant byte comes first.
Four bytes such as 01 02 03 04 mean different numeric values depending on the order rule.
That is why the first question should always be:
- what numeric type is expected
- what endianness is used
Without that, the same byte array can legitimately map to different numbers.
Use ByteBuffer for Standard Cases
ByteBuffer is the cleanest built-in tool for fixed-width numeric conversions.
Big-endian int example:
Little-endian example:
This is the recommended baseline when the array length matches a standard primitive type.
Manual Bit Shifting
If you want full control or need to understand what is happening under the hood, you can combine bytes manually. The key detail is masking each byte with 0xFF so Java does not sign-extend negative byte values.
That example assumes big-endian order.
Little-endian is the reverse:
Converting to long
For eight bytes, use getLong() or build the value with long shifts.
If the protocol uses unsigned 32-bit values but Java stores them in long, that is also a common pattern.
Use BigInteger for Larger Values
If the byte array is longer than eight bytes or represents a number larger than a primitive can hold, use BigInteger.
The leading 1 tells BigInteger to interpret the bytes as an unsigned positive value. Without that, the highest bit may be treated as a sign bit.
Validate the Array Length
Do not assume the byte array has the right size. Validate before converting.
That matters because ByteBuffer.getInt() and similar methods expect enough bytes to be present. Protocol bugs are easier to diagnose with explicit checks than with downstream buffer exceptions.
Common Pitfalls
- Ignoring endianness and decoding the same bytes in the wrong order.
- Forgetting
& 0xFFwhen combining signed Java bytes manually. - Using
intwhen the value is logically unsigned and may exceed the signed range. - Passing large unsigned byte arrays into
BigIntegerwithout forcing a positive sign. - Converting without checking that the array length matches the expected numeric type.
Summary
- The correct conversion depends on both numeric type and byte order.
- Use
ByteBufferfor clean fixed-width conversions. - Use manual bit shifting only when you need explicit control.
- Mask bytes with
0xFFto avoid signed-byte surprises. - Use
BigIntegerfor values larger than Java primitive types can hold.
Related reading
- How to convert a char array back to a string?
- How to convert a Collection to List?
- How to convert a column of DataTable to a List
- How to convert a dataframe to a dictionary
- How to convert a char to a String?
- How to convert a double to long without casting?
- How to convert a Java 8 Stream to an Array?
- How to convert a JSON string to a dictionary?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.