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.

Discussions

java - How to Convert Int to Unsigned Byte and Back - Stack Overflow
I need to convert a number into an unsigned byte. The number is always less than or equal to 255, and so it will fit in one byte. I also need to convert that byte back into that number. How would ... More on stackoverflow.com
🌐 stackoverflow.com
Java: Convert byte to integer - Stack Overflow
The most common will be storing ... pair of bytes. Converting that back to an integer depends on whether you store it big-endian form: ... It was worth mentioning since a lot of projects use it anyway. ... Works great on Bluetooth command conversions etc. No need to worry about signed vs. unsigned conversion. ... Note there's a constructor in this class where you can manually specify you wish to handle unsigned values. 2019-06-29T12:08:42.6Z+00:00 ... import java.io.*; public ... More on stackoverflow.com
🌐 stackoverflow.com
June 20, 2013
java - Convert Byte[] to int - Stack Overflow
I have this method that converts a signed or non-signed byte to int, but it doesn't return what it is supposed to return. Can someone point out the issue in the below code? public int convertByteT... More on stackoverflow.com
🌐 stackoverflow.com
November 7, 2011
types - Convert a byte array to integer in Java and vice versa - Stack Overflow
I want to store some data into byte arrays in Java. Basically just numbers which can take up to 2 Bytes per number. I'd like to know how I can convert an integer into a 2 byte long byte array and vice versa. I found a lot of solutions googling but most of them don't explain what happens in the code. More on stackoverflow.com
🌐 stackoverflow.com
October 1, 2011
🌐
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
🌐
Way2Java
way2java.com › home › byte to int java
byte to int Java
November 21, 2015 - public class Conversions { public static void main(String args[]) { byte b1 = 10; // 1 byte int i1 = b1; // one byte to int 4 bytes System.out.println("byte value: " + b1); // prints 10 System.out.println("Converted int value: " + i1); // prints 10 } } ... 1 byte value b1 is assigned to 4 bytes ...
🌐
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.

🌐
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.
🌐
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