1
votes

I am doing an encryption cipher which requires to read a hex key from file and convert it to bytes[] array.

The only way to do it, as I have found out, is to read lines from a file (with bufferedReader), create hex String array from them and convert each hex string array member to a binary string which can later be converted to a byte using parseByte.

To be more clear:

  • read line
  • get hex array
  • convert to binary
  • convert to byte)

So, basically, it looks like this: hex[i] is a string from hex array.

requiredByte[i] = Byte.parseByte(hexToBinary(hex[i]), 2);

So, to my method to work I need to read hex key from file and convert it into binary strings.

Key in hex looks like this (yes, both lines are the same):

01 23 45 67 89 AB CD EF

01 23 45 67 89 AB CD EF

I need to convert it to this binary Key:

0000 0001 0010 0011 0100 0101 0110 0111

1000 1001 1010 1011 1100 1101 1110 1111

0000 0001 0010 0011 0100 0101 0110 0111

1000 1001 1010 1011 1100 1101 1110 1111

However, the fifth hex number is 89, what is 137 as an integer. And you can only convert numbers between -127 and 127 into a binary string.

When I try to convert hex number 89 in my own way, I get this message in console:

Exception in thread "main" java.lang.NumberFormatException: Value out of range. Value:"100011" Radix:16

Do you guys have any ideas of how I could convert hex to binary or even how could I convert hex String[] array to byte[] array?

2
represent each digit as 4 bits (aka a nibble). Then your "fifth hex number" is 8 and 9 (or 1000 and 1001).Elliott Frisch
@ElliottFrisch could you explain more on how to do this? I've tried searching on how to represent numbers as 4 bits but failed. Any information would be highly appreciatedzilijonas
you absolutely do not need to convert the hex values to binary before converting them to bytes; you have several options to avoid doing thatlandru27
@LiJonas : your question begins with "I am doing an encryption cipher which requires to read a hex key from file and convert it to bytes[] array."; that other post begins with "I am looking for a way to convert a long string (from a dump), that represents hex values into a byte array." can you explain how these are different?landru27

2 Answers

3
votes

I recommend you use a fairly simple method BigInteger::toString(int radix) which returns the String representation in the given radix. Use 2 for the binary representation.

// 100100011010001010110011110001001101010111100110111101111
new BigInteger("0123456789ABCDEF", 16).toString(2);

Note the String must be blank characters free and using this way you have to process them from the array each one-by-one.

2
votes

Instead of parsing using Byte.parseByte using a larger type such as Integer. Integer.parseInt will work fine, and you can just cast the result to a byte if that's what you need.

But remember that printing that byte will show you a negative number. If you want to see it as an unsigned value, do a bitwise and with 0xff like this:

byte b = 0xff;
System.out.println(b & 0xff); //Will print 255 instead of -1