How to convert byte array to hex string which it is using for convert it to long?

I try to convert byte[] to long, but I got java.lang.NumberFormatException.

this is the byte array I wanna convert to long

byte[] data = new byte[]{(byte) 0xDB, (byte) 0xA7, 0x53, (byte) 0xF8, (byte) 0xA8, 0x0C, 0x66, 0x8};

Then I try to convert that data to hex string with this algorithm

public static String hexStringFromHexData(byte[] data){
        StringBuilder hexString = new StringBuilder();

        for (byte subData : data) {
            String temp = Integer.toHexString(subData & 0xFF);
            if (temp.length() == 1) {
                temp = "0" + temp;
            }
            hexString.append(temp);
        }

        return hexString.toString();
    }

So I got the hexstring like this “dba753f8a80c6608”

with that hexstring I convert it to long like below

String hexstring = "dba753f8a80c6608";
Long value = Long.parseLong(hexString.trim(), 16);

It throw NumberFormatException because the hexstring is exceed 7fffffffffffffff (Long.MAX_VALUE).

The right hexstring have to be like this “DBA753F8A80C668” and with this hexstring is able to convert it to this long value 989231983928329832L without get exception.

we can try convert this long value back to hex string

long value = 989231983928329832L
String hexString = Long.toHexString(value);

the we got the hexstring like this “DBA753F8A80C668” and if we convert it to byte array it will be same with byte array data like above.

So how do I convert the byte[] data above to hex string like this “DBA753F8A80C668”?

Is it any other algorithm for convert byte[] data above and return correct long value like this 989231983928329832L?

======================================================================

simple step to reproduce:

  1. 989231983928329832 to convert it to byte[] data

  2. convert byte[] data back to long value 989231983928329832L

Your byte array is incorrect.

    byte[] data = new byte[]{(byte) 0xDB, (byte) 0xA7, 0x53, (byte) 0xF8, (byte) 0xA8, 0x0C, 0x66, 0x8};
    long a = 989231983928329832L;
    byte[] bytes = ByteBuffer.allocate(8).putLong(a).array();
    for(int i = 0; i<8; i++){
        System.out.printf("%02x/t%02x/n", data[i], bytes[i]);
    }
    System.out.println(Long.parseLong(hexStringFromHexData(bytes), 16));

Which outputs.

db 0d

a7 ba

53 75

f8 3f

a8 8a

0c 80

66 c6

08 68

989231983928329832

The first column is your byte array, the second column is the one created. You can also see your string technique works with the correct byte array.

The problem you are facing is due to the face that the long type in Java is signed, so the maximum absolute value you can have is indeed 0x7fffffffffffffff, smaller that the desired 0xDBA753F8A80C6608.

You can solve the issue in several ways:

  • using BigInteger: BigInteger longValue = new BigInteger(1, data);

  • if you can work with Java 8, you can use unsigned long, as pointed out in comments from @saka1029: long value = Long.parseUnsignedLong(hexString.trim(), 16);

EDIT: after some research, I warn you that the output of the second method will appear negative if printed. This is due to the fact that the long variable is still signed, and only some methods of the Long class treat is as unsigned. Here is the source I used