Java
Byte Array
Numeric Conversion
Programming
Java Tutorial

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.

Practice algorithms

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:

java
1import java.nio.ByteBuffer;
2
3byte[] data = new byte[] {0x00, 0x00, 0x01, 0x2C};
4int value = ByteBuffer.wrap(data).getInt();
5
6System.out.println(value); // 300

Little-endian example:

java
1import java.nio.ByteBuffer;
2import java.nio.ByteOrder;
3
4byte[] data = new byte[] {0x2C, 0x01, 0x00, 0x00};
5int value = ByteBuffer.wrap(data)
6    .order(ByteOrder.LITTLE_ENDIAN)
7    .getInt();
8
9System.out.println(value); // 300

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.

java
1byte[] data = new byte[] {0x00, 0x00, 0x01, 0x2C};
2
3int value =
4    ((data[0] & 0xFF) << 24) |
5    ((data[1] & 0xFF) << 16) |
6    ((data[2] & 0xFF) << 8)  |
7    (data[3] & 0xFF);
8
9System.out.println(value); // 300

That example assumes big-endian order.

Little-endian is the reverse:

java
1byte[] data = new byte[] {0x2C, 0x01, 0x00, 0x00};
2
3int value =
4    (data[0] & 0xFF) |
5    ((data[1] & 0xFF) << 8) |
6    ((data[2] & 0xFF) << 16) |
7    ((data[3] & 0xFF) << 24);
8
9System.out.println(value); // 300

Converting to long

For eight bytes, use getLong() or build the value with long shifts.

java
1import java.nio.ByteBuffer;
2
3byte[] data = new byte[] {
4    0x00, 0x00, 0x00, 0x00,
5    0x00, 0x00, 0x01, 0x2C
6};
7
8long value = ByteBuffer.wrap(data).getLong();
9System.out.println(value); // 300

If the protocol uses unsigned 32-bit values but Java stores them in long, that is also a common pattern.

java
1byte[] data = new byte[] {(byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF};
2
3long unsigned32 =
4    ((long)(data[0] & 0xFF) << 24) |
5    ((long)(data[1] & 0xFF) << 16) |
6    ((long)(data[2] & 0xFF) << 8) |
7    ((long)(data[3] & 0xFF));
8
9System.out.println(unsigned32); // 4294967295

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.

java
1import java.math.BigInteger;
2
3byte[] data = new byte[] {0x01, 0x00, 0x00, 0x00, 0x00};
4BigInteger value = new BigInteger(1, data);
5
6System.out.println(value); // 4294967296

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.

java
if (data.length != 4) {
    throw new IllegalArgumentException("Expected exactly 4 bytes for an int");
}

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 & 0xFF when combining signed Java bytes manually.
  • Using int when the value is logically unsigned and may exceed the signed range.
  • Passing large unsigned byte arrays into BigInteger without 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 ByteBuffer for clean fixed-width conversions.
  • Use manual bit shifting only when you need explicit control.
  • Mask bytes with 0xFF to avoid signed-byte surprises.
  • Use BigInteger for values larger than Java primitive types can hold.

Related reading
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.