Your array is of byte primitives, but you're trying to call a method on them.

You don't need to do anything explicit to convert a byte to an int, just:

int i=rno[0];

...since it's not a downcast.

Note that the default behavior of byte-to-int conversion is to preserve the sign of the value (remember byte is a signed type in Java). So for instance:

byte b1 = -100;
int i1 = b1;
System.out.println(i1); // -100

If you were thinking of the byte as unsigned (156) rather than signed (-100), as of Java 8 there's Byte.toUnsignedInt:

byte b2 = -100; // Or `= (byte)156;`
int i2 = Byte.toUnsignedInt(b2);
System.out.println(i2); // 156

Prior to Java 8, to get the equivalent value in the int you'd need to mask off the sign bits:

byte b2 = -100; // Or `= (byte)156;`
int i2 = (b2 & 0xFF);
System.out.println(i2); // 156

Just for completeness #1: If you did want to use the various methods of Byte for some reason (you don't need to here), you could use a boxing conversion:

Byte b = rno[0]; // Boxing conversion converts `byte` to `Byte`
int i = b.intValue();

Or the Byte constructor:

Byte b = new Byte(rno[0]);
int i = b.intValue();

But again, you don't need that here.


Just for completeness #2: If it were a downcast (e.g., if you were trying to convert an int to a byte), all you need is a cast:

int i;
byte b;

i = 5;
b = (byte)i;

This assures the compiler that you know it's a downcast, so you don't get the "Possible loss of precision" error.

Answer from T.J. Crowder on Stack Overflow
๐ŸŒ
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 IntToByteArrayExample { public static void main(String[] args) { int num = 1; byte[] result = convertIntToByteArray(num); System.out.println("Input : " + num); System.out.println("Byte Array (Hex) : " + convertBytesToHex(result)); } // method 1, int need 4 bytes, default ByteOrder.BIG_ENDIAN public static byte[] convertIntToByteArray(int value) { return ByteBuffer.allocate(4).putInt(value).array(); } // method 2, bitwise right shift public static byte[] convertIntToByteArray2(int value) { return new byte[] { (byte)(value >> 24),
Top answer
1 of 7
110

Your array is of byte primitives, but you're trying to call a method on them.

You don't need to do anything explicit to convert a byte to an int, just:

int i=rno[0];

...since it's not a downcast.

Note that the default behavior of byte-to-int conversion is to preserve the sign of the value (remember byte is a signed type in Java). So for instance:

byte b1 = -100;
int i1 = b1;
System.out.println(i1); // -100

If you were thinking of the byte as unsigned (156) rather than signed (-100), as of Java 8 there's Byte.toUnsignedInt:

byte b2 = -100; // Or `= (byte)156;`
int i2 = Byte.toUnsignedInt(b2);
System.out.println(i2); // 156

Prior to Java 8, to get the equivalent value in the int you'd need to mask off the sign bits:

byte b2 = -100; // Or `= (byte)156;`
int i2 = (b2 & 0xFF);
System.out.println(i2); // 156

Just for completeness #1: If you did want to use the various methods of Byte for some reason (you don't need to here), you could use a boxing conversion:

Byte b = rno[0]; // Boxing conversion converts `byte` to `Byte`
int i = b.intValue();

Or the Byte constructor:

Byte b = new Byte(rno[0]);
int i = b.intValue();

But again, you don't need that here.


Just for completeness #2: If it were a downcast (e.g., if you were trying to convert an int to a byte), all you need is a cast:

int i;
byte b;

i = 5;
b = (byte)i;

This assures the compiler that you know it's a downcast, so you don't get the "Possible loss of precision" error.

2 of 7
49
byte b = (byte)0xC8;
int v1 = b;       // v1 is -56 (0xFFFFFFC8)
int v2 = b & 0xFF // v2 is 200 (0x000000C8)

Most of the time v2 is the way you really need.

๐ŸŒ
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 - Converting byte to int is a common operation, especially when dealing with low-level data manipulation, file I/O, or network communication. In this article, weโ€™ll explore various methods through which we can achieve the byte to int conversion. In Java, byte and int are fundamental data types ...
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ java โ€บ java byte to int
How to Convert Byte to Int in Java | Delft Stack
February 2, 2024 - Letโ€™s delve into a practical ... = 42; // Step 2: Convert byte to int using BigInteger int intValue = new BigInteger(new byte[] {byteValue}).intValue(); // Step 3: Display the results System.out.println("Original Byte ...
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java array โ€บ convert a byte array to a numeric representation in java
Convert a Byte Array to a Numeric Representation in Java | Baeldung
August 7, 2025 - The main purpose of the java.math.BigInteger class is to represent large numeric values that would otherwise not fit within a primitive data type. Even though we can use it to convert between a byte array and a primitive value, using BigInteger is a bit heavy for this kind of purpose. Now, letโ€™s use the BigInteger class to convert a byte array to an int value:
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ bytes โ€บ java-convert-int-to-a-byte
Java: Convert Int to a Byte
June 22, 2023 - In Java, an int is a 32-bit signed integer, while a byte is a 8-bit signed integer. Converting an int to a byte can be useful in certain situations, such as whe...
Find elsewhere
๐ŸŒ
Programming.Guide
programming.guide โ€บ java โ€บ convert-unsigned-byte-to-int.html
Convert unsigned byte to int in Java | Programming.Guide
Use Byte.toUnsignedInt to avoid sign extension. byte b = (byte) 0b10010110; // -106 or 150 int signed = b; // -106 (with sign extension) int unsigned = Byte.toUnsignedInt(b); // 150 (without sign extension)
๐ŸŒ
Real's HowTo
rgagnon.com โ€บ javadetails โ€บ java-0026.html
Convert an UNSIGNED byte to a JAVA integer - Real's Java How-to
public class UnsignedByte { public static void main (String args[]) { byte b1 = 127; byte b2 = -128; byte b3 = -1; System.out.println(b1); System.out.println(b2); System.out.println(b3); System.out.println(unsignedByteToInt(b1)); System.out.println(unsignedByteToInt(b2)); System.out.println(unsignedByteToInt(b3)); /* 127 -128 -1 127 128 255 */ } public static int unsignedByteToInt(byte b) { return (int) b & 0xFF; } }Therefore for an array of 4 bytes (buf[]), which represents an integer : int i = 0; int pos = 0; i += unsignedByteToInt(buf[pos++]) << 24; i += unsignedByteToInt(buf[pos++]) << 16; i += unsignedByteToInt(buf[pos++]) << 8; i += unsignedByteToInt(buf[pos++]) << 0; To convert a byte to it's hexadecimal equivalent
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 377611 โ€บ java โ€บ converting-byte-int
converting byte[] to int (Java in General forum at Coderanch)
I came up with a very easy solution which is to shift the byte depending on their position and OR it with the next byte to create my int. example int test = (data[7] >> 8) | data[8]; data[7] contains 02 in hex data[8] contains B8 in hex the int value theoratically should be 696, becuse that ...
๐ŸŒ
Navigatone
navigatone.com โ€บ 2024 โ€บ 04 โ€บ 29 โ€บ four-ways-to-convert-a-byte-array-to-an-integer
Convert a Byte Array to an Integer in Java โ€“ Navigatone
May 5, 2024 - byte[] byteArray = new byte[] {1, 2, 3, 4}; int intValue = new BigInteger(byteArray).intValue(); System.out.println(intValue); System.out.println(Integer.toBinaryString(intValue)); But also in this method there is some overhead as new objects ...
Top answer
1 of 9
267

Use the classes found in the java.nio namespace, in particular, the ByteBuffer. It can do all the work for you.

byte[] arr = { 0x00, 0x01 };
ByteBuffer wrapped = ByteBuffer.wrap(arr); // big-endian by default
short num = wrapped.getShort(); // 1

ByteBuffer dbuf = ByteBuffer.allocate(2);
dbuf.putShort(num);
byte[] bytes = dbuf.array(); // { 0, 1 }
2 of 9
157
byte[] toByteArray(int value) {
     return  ByteBuffer.allocate(4).putInt(value).array();
}

byte[] toByteArray(int value) {
    return new byte[] { 
        (byte)(value >> 24),
        (byte)(value >> 16),
        (byte)(value >> 8),
        (byte)value };
}

int fromByteArray(byte[] bytes) {
     return ByteBuffer.wrap(bytes).getInt();
}
// packing an array of 4 bytes to an int, big endian, minimal parentheses
// operator precedence: <<, &, | 
// when operators of equal precedence (here bitwise OR) appear in the same expression, they are evaluated from left to right
int fromByteArray(byte[] bytes) {
     return bytes[0] << 24 | (bytes[1] & 0xFF) << 16 | (bytes[2] & 0xFF) << 8 | (bytes[3] & 0xFF);
}

// packing an array of 4 bytes to an int, big endian, clean code
int fromByteArray(byte[] bytes) {
     return ((bytes[0] & 0xFF) << 24) | 
            ((bytes[1] & 0xFF) << 16) | 
            ((bytes[2] & 0xFF) << 8 ) | 
            ((bytes[3] & 0xFF) << 0 );
}

When packing signed bytes into an int, each byte needs to be masked off because it is sign-extended to 32 bits (rather than zero-extended) due to the arithmetic promotion rule (described in JLS, Conversions and Promotions).

There's an interesting puzzle related to this described in Java Puzzlers ("A Big Delight in Every Byte") by Joshua Bloch and Neal Gafter . When comparing a byte value to an int value, the byte is sign-extended to an int and then this value is compared to the other int

byte[] bytes = (โ€ฆ)
if (bytes[0] == 0xFF) {
   // dead code, bytes[0] is in the range [-128,127] and thus never equal to 255
}

Note that all numeric types are signed in Java with exception to char being a 16-bit unsigned integer type.

๐ŸŒ
YouTube
youtube.com โ€บ shorts โ€บ N2yWhNiv5JM
How To Convert Byte To Int In Java - YouTube
December 7, 2024 - This video demonstrates how to convert byte to int in Java.#java #learntocode #codingtutorial
๐ŸŒ
Pete Freitag
petefreitag.com โ€บ blog โ€บ unsigned-byte-array-int-java
Converting an unsigned byte array to an integer in Java
December 8, 2020 - You can use java.nio.ByteBuffer of you want to convert an array of bytes into a stream of integers.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ byte-intvalue-method-in-java-with-examples
Byte intValue() method in Java with examples - GeeksforGeeks
July 11, 2025 - The intValue() method of Byte class is a built in method in Java which is used to return the value of this Byte object as int.