0

So i have a piece of code, which converted byte[] from a binary file into int[] which then went on to be converted to String.

Example: 02 09 1A would translate to "2926" using String.valueOf(byte);

But this is where the fun begins: the old Code already split up the array before it became String so it didn't matter. Now i have code which needs to somehow figure out if there is a 2 9 or 29... How can i get the String to
- stay in Hex instead of "switching" to decimal and
- keep the zeros in the string
so i can always grab the next two chars, figure out which info they display and go on?

Input:
05 06 1D 11 07 08 01 32 21 28 2F 20 2E 21 34 22 25 33 01 02 09 0A FF 0B 0C
2!(/ .!4"%3 ÿ
Current Output: 562917781503340473246335234375112910-11112
Expected Output: 05061D110708013221282F202E21342225330102090AFF0B0C

(Didn't put my old code in here since it's based on int which is crap)

RAVN Mateus
  • 560
  • 3
  • 13
  • 2
    Can you show some inputs and outputs? Right now your question is not very clear. – Sweeper May 23 '20 at 13:33
  • Added Input, expected and real output. Also to clarify: java makes FF -1 so the old code stopped reading here, but its inevidable to use FF at this point of the binary. – RAVN Mateus May 23 '20 at 13:46
  • Is the input in a `byte[]`? Also, why is "2!(/ .!4"%3 ÿ" at the end of the input? – Sweeper May 23 '20 at 13:49
  • Thanks for the answer, this code works exactly the way i wanted ^^ The reason this random stuff is at the end of input is, that i put the ASCII characters representing the hex value under the hex value so noone could think "05 " would be the bytes 0x30 0x35 0x20) – RAVN Mateus May 23 '20 at 14:17

2 Answers2

1

If I understand correctly, you have a byte[] as input, and you want to output a string that is all those bytes, in hex form, joined together:

private static String byteArrayToString(byte[] byteArray) {
    StringBuilder builder = new StringBuilder();
    for (byte b : byteArray) {
        int i = Byte.toUnsignedInt(b);
        if (i < 16) {
            builder.append("0");
        }
        builder.append(Integer.toHexString(i).toUpperCase());
    }
    return builder.toString();
}

Note the use of Byte.toUnsignedInt. This converts things like -1 the byte to the int 0xff. This is required because Java's bytes are signed.

Also note where I pad with 0. I only do this if the byte is a single digit in hex. i < 16 is the condition to check this.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
1

Another way:

  char[] hexVals = {'0', '1','2','3','4','5','6','7','8','9','A', 'B','C','D','E','F'};
  byte[] bytes = {5, 6, (byte)0xAB, (byte)0xde};   // sample input
  char[] output = new char[bytes.length*2];
  // split each byte's hex digits into two chars
  for(int i=0; i < bytes.length; i++) {
     output[2*i] = hexVals[(bytes[i] >> 4) & 0xf];  // HO nibble
     output[2*i+1] = hexVals[bytes[i] & 0xf];       // LO nibble
  }
  System.out.println(new String(output)); //  0506ABDE
NormR
  • 336
  • 3
  • 12
  • Also a pretty good solution, since i have to write the same code in Assembler, C, C++ and Java i will definitly make use of it in the other languages where i cant access the libraries. I will leave the accepted answer to Sweeper since this Post was decleared Java but thank you for showing me this (i cant access any libraries in c and assembler since the code is for reading header used in a java game and also for all kinds of files in my OS) :) – RAVN Mateus May 23 '20 at 15:10