java
integer
byte array
conversion
programming

Java integer to byte array

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 an int to a byte array in Java is usually a matter of choosing the right byte order and API. The two common approaches are ByteBuffer for clarity and manual bit shifting when you want explicit control.

What You Are Really Converting

A Java int is a 32-bit signed value, so the full conversion always involves 4 bytes.

That means if you want a complete representation, the output array length should be 4:

java
System.out.println(Integer.BYTES); // 4

The next question is byte order, also called endianness.

The Cleanest Option: ByteBuffer

ByteBuffer is the standard library tool for this job.

java
1import java.nio.ByteBuffer;
2
3public class Main {
4    public static byte[] intToBytes(int value) {
5        return ByteBuffer.allocate(Integer.BYTES)
6                .putInt(value)
7                .array();
8    }
9
10    public static void main(String[] args) {
11        byte[] bytes = intToBytes(16909060);
12        for (byte b : bytes) {
13            System.out.print((b & 0xFF) + " ");
14        }
15    }
16}

This prints 1 2 3 4 because 16909060 is 0x01020304.

ByteBuffer defaults to big-endian order, which is often what you want for network protocols and many binary formats.

Changing Endianness

If you need little-endian order, say so explicitly.

java
1import java.nio.ByteBuffer;
2import java.nio.ByteOrder;
3
4public class Main {
5    public static byte[] intToLittleEndianBytes(int value) {
6        return ByteBuffer.allocate(Integer.BYTES)
7                .order(ByteOrder.LITTLE_ENDIAN)
8                .putInt(value)
9                .array();
10    }
11}

Never assume the receiver expects the same byte order you happen to use locally. Binary bugs often come from mismatched endianness, not from the conversion code itself.

Manual Bit Shifting

If you want full control or want to avoid ByteBuffer allocation patterns in a hot path, manual shifting is straightforward.

java
1public class Main {
2    public static byte[] intToBytesManual(int value) {
3        return new byte[] {
4            (byte) (value >>> 24),
5            (byte) (value >>> 16),
6            (byte) (value >>> 8),
7            (byte) value
8        };
9    }
10}

This version produces big-endian output.

For little-endian, reverse the order:

java
1public static byte[] intToBytesLittleEndianManual(int value) {
2    return new byte[] {
3        (byte) value,
4        (byte) (value >>> 8),
5        (byte) (value >>> 16),
6        (byte) (value >>> 24)
7    };
8}

Converting Back To An Integer

It is often useful to show the reverse operation too:

java
1import java.nio.ByteBuffer;
2
3public class Main {
4    public static int bytesToInt(byte[] bytes) {
5        return ByteBuffer.wrap(bytes).getInt();
6    }
7}

If you encode with a certain byte order, decode with the same one.

Signed Bytes In Java

Java's byte type is signed, which can surprise people when printing values. A byte with binary value 11111111 prints as -1, not 255.

That does not mean the conversion is wrong. If you want the unsigned numeric view for debugging, use:

java
int unsigned = b & 0xFF;

That is why example code often prints (b & 0xFF) rather than b directly.

When To Use Which Approach

A practical rule is:

  • use ByteBuffer for readability and standard library correctness
  • use manual shifts when you need explicit control or are implementing a protocol by hand

In most application code, ByteBuffer is the clearer option.

Common Pitfalls

The most common mistake is forgetting to define the byte order. Two systems can both convert correctly and still disagree because one is big-endian and the other is little-endian.

Another mistake is printing signed byte values and assuming the negative output means corruption.

Developers also sometimes allocate arrays of the wrong size. A Java int always needs 4 bytes.

Finally, do not mix encoding and decoding styles casually. If the writer is little-endian and the reader assumes big-endian, the numbers will be wrong even though both sides use valid code.

Summary

  • A Java int converts to 4 bytes.
  • 'ByteBuffer is the standard and usually clearest approach.'
  • Java ByteBuffer defaults to big-endian unless you change it.
  • Manual bit shifting is fine when you want explicit control.
  • Always match byte order on both the writing and reading sides.

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.