EvilZone
Programming and Scripting => Java => : red.evil January 04, 2014, 04:55:13 PM
-
Hey guys,
for my current project I need to convert a byte array to a hex value, since I had problems with solving it I googled and found this algorithm:
public static BigInteger convertToBigInteger( byte[] toConvert )
{
BigInteger converted = BigInteger.valueOf( 0 );
for( int i = 0; i < toConvert.length; i++ )
{
converted = converted.shiftLeft( 8 );
converted = converted.add( BigInteger.valueOf( toConvert[i] & 0xff ) );
}
return( converted );
}
So I will get a BigInteger which I can simply convert to a Hex-String.
But I don't get why I have to do this operation: toConvert & 0xFF
Does somebody of you know why I have to do it?
Regards,
red.evil
*staff note: Do not double post!
-
It's a byte mask. 0xFF is decimal for 255. The code applies a boolean AND operation to retrieve the least significant byte (last 8 bits).
-
Thank you, but that wasn't my problem. My problem was to understand why I need to do this logic operationm, but I have a solution to this question now.
It is because normaly byte is signed in Java. If we do such an operation it will be casted to an also signed int and will be interpreted as two's complement. But what we want in this case is an unsigned byte and don't want to lose our byte pattern. To save the byte pattern, we can make the logic operation somebyte & 0xff.
So my mistake was to not consider that we have a signed variable here.
-
Thank you, but that wasn't my problem. My problem was to understand why I need to do this logic operationm, but I have a solution to this question now.
It is because normaly byte is signed in Java. If we do such an operation it will be casted to an also signed int and will be interpreted as two's complement. But what we want in this case is an unsigned byte and don't want to lose our byte pattern. To save the byte pattern, we can make the logic operation somebyte & 0xff.
So my mistake was to not consider that we have a signed variable here.
This is correct.
However, it seems a bit over the top to use a BigInteger for a simple hex conversion. You can use Integer.toHexString(n) or Integer.toString(n, 16) to achieve the same.
Example (with some formatting):
protected static String convertByteToHex(byte array[]) {
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < array.length; i++) {
if ((array[i] & 0xff) < 0x10) {
buffer.append("0");
}
buffer.append(Integer.toString(array[i] & 0xff, 16) + " ");
}
return buffer.toString().trim();
}
-
I've tried Integer and Long already, but they're too small, because I need those calculations for Hash- and Checksums (e.g. SHA-512).
-
I've tried Integer and Long already, but they're too small, because I need those calculations for Hash- and Checksums (e.g. SHA-512).
Have a close look at the method I wrote.
There is only one byte at a time converted to an integer. That means it can't be too big.
The StringBuilder builds up the string byte by byte.
BigInteger is pretty heavy when it comes to calculating with them, like addition, substraction and so on. You don't need that unless you really have to do something else with the numbers afterwards despite string conversion.
-
Oh, sorry I've misread your code snippet..
Initially I wanted BigIntegers also to save the value, but I think with your method I will just use the given byte-array as it is.
Thank you all for you help!