How to Convert a Byte Array to Its Numeric Value (Java)

How to convert a byte array to its numeric value (Java)?

Assuming the first byte is the least significant byte:

long value = 0;
for (int i = 0; i < by.length; i++)
{
value += ((long) by[i] & 0xffL) << (8 * i);
}

Is the first byte the most significant, then it is a little bit different:

long value = 0;
for (int i = 0; i < by.length; i++)
{
value = (value << 8) + (by[i] & 0xff);
}

Replace long with BigInteger, if you have more than 8 bytes.

Thanks to Aaron Digulla for the correction of my errors.

How do I convert a byte array to a long in Java?

For the endianness, test with some numbers you know, and then you will be using a byte shifting to move them into the long.

You may find this to be a starting point.
http://www.janeg.ca/scjp/oper/shift.html

The difficulty is that depending on the endianess will change how you do it, but you will shift by 24, 16, 8 then add the last one, basically, if doing 32 bits, but you are going longer, so just do extra shifting.

Fast way converting a byte array to corresponding Integer

try this

    int res = 0;
for(int i = foo.length -1, m = 1; i >=0; i--, m *= 10) {
res += (foo[i] & 0xF) * m;
}

Convert a byte array to integer in Java and vice versa

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 }

How to convert byte array into integer value

Just use ByteBuffer.getInt():

int pomAsInt = ByteBuffer.wrap(pom).getInt();

Java: ByteArray to positive number and vice versa conversion

Edit: just replace

String string = new BigInteger(originalBytes).toString();

with

String string = new BigInteger(1, originalBytes).toString();

The 1, signals that the passed array represents a positive number (signum = 1)

Original:

You can just prefix the array with a zero byte:

byte[] original = new byte[] { (byte) 255 };

System.out.println(new BigInteger(original).toString()); // prints "-1"

byte[] paddedCopy = new byte[original.length + 1];
for (int i = 0; i < original.length; i++) {
paddedCopy[i + 1] = original[i];
}
System.out.println(new BigInteger(paddedCopy).toString()); // prints "255"

This will essentially nullify the sign bit, making the number unsigned.

How BigInteger convert a byte array to number in Java?

The number 1826 is, in binary, 11100100010.
If you split that in groups of 8 bits, you get the following:

00000111 00100010

Which are the numbers 7 and 34

Create a 48 bit signed value from a byte array in Java

First you need to understand sign extension. You need to treat each byte as an unsigned value.

      long v = 0;
byte q = -2; // unsigned value of 254
v = v + q;
System.out.println(v); // prints -2 which is not what you want.
v = 0;
v = v + (q & 0xFF); // mask off sign extension from q.
System.out.println(v); // prints 254 which is correct.

This is one way of doing it.

     long val = 0;
byte[] bytes = { -1, 12, 99, -121, -3, 123
};
for (int i = 0; i < bytes.length; i++) {
// shift the val left by 8 bits first.
// then add b. You need to mask it with 0xFF to
// eliminate sign extension to a long which will
// result in an incorrect conversion.
val = (val << 8) | ((i == 0 ? bytes[i]
: bytes[i] & 0xFF));
}
System.out.println(val);

Convert Byte Array to Int odd result Java and Kotlin

As suggested by @Lother and itsme86.

fun littleEndianConversion(bytes: ByteArray): Int {
var result = 0
for (i in bytes.indices) {
result = result or (bytes[i].toInt() shl 8 * i)
}
return result
}


Related Topics



Leave a reply



Submit