Author Topic: Question: byte array - to - hex - algorithm  (Read 1983 times)

0 Members and 1 Guest are viewing this topic.

Offline red.evil

  • /dev/null
  • *
  • Posts: 16
  • Cookies: 1
    • View Profile
Question: byte array - to - hex - algorithm
« on: 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:
Code: (Java) [Select]
    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!
« Last Edit: January 04, 2014, 05:50:37 pm by Phage »

Offline vezzy

  • Royal Highness
  • ****
  • Posts: 771
  • Cookies: 172
    • View Profile
Re: Question: byte array - to - hex - algorithm
« Reply #1 on: January 04, 2014, 05:35:28 pm »
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).
Quote from: Dippy hippy
Just brushing though. I will be semi active mainly came to find a HQ botnet, like THOR or just any p2p botnet

Offline red.evil

  • /dev/null
  • *
  • Posts: 16
  • Cookies: 1
    • View Profile
Re: Question: byte array - to - hex - algorithm
« Reply #2 on: January 04, 2014, 06:38:43 pm »
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.

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Question: byte array - to - hex - algorithm
« Reply #3 on: January 04, 2014, 09:18:04 pm »
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):

Code: (Java) [Select]
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();
}

Offline red.evil

  • /dev/null
  • *
  • Posts: 16
  • Cookies: 1
    • View Profile
Re: Question: byte array - to - hex - algorithm
« Reply #4 on: January 04, 2014, 09:32:03 pm »
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).

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Question: byte array - to - hex - algorithm
« Reply #5 on: January 05, 2014, 10:45:33 am »
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.
« Last Edit: January 05, 2014, 10:46:45 am by Deque »

Offline red.evil

  • /dev/null
  • *
  • Posts: 16
  • Cookies: 1
    • View Profile
Re: Question: byte array - to - hex - algorithm
« Reply #6 on: January 05, 2014, 01:56:16 pm »
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!