1

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?

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
zilijonas
  • 3,285
  • 3
  • 26
  • 49
  • represent each digit as 4 bits (aka a [nibble](https://en.wikipedia.org/wiki/Nibble)). Then your "fifth hex number" is `8` and `9` (or `1000` and `1001`). – Elliott Frisch May 22 '18 at 21:53
  • 1
    Possible duplicate of [Convert a string representation of a hex dump to a byte array using Java?](https://stackoverflow.com/questions/140131/convert-a-string-representation-of-a-hex-dump-to-a-byte-array-using-java) – landru27 May 22 '18 at 21:57
  • @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 appreciated – zilijonas May 22 '18 at 21:59
  • you absolutely do **not** need to convert the hex values to binary before converting them to bytes; you have several options to avoid doing that – landru27 May 22 '18 at 21:59
  • @landru27 saw that post, didn't find an answer to my question. Sorry if it looks like a duplicate – zilijonas May 22 '18 at 21:59
  • @landru27 could you give me some examples of how can I convert hex string directly into byte? – zilijonas May 22 '18 at 22:01
  • 1
    @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 May 22 '18 at 22:03
  • @landru27 the main answer was not working when I was testing so I presumed my issue is different. I will double check this later and delete my post in this case. – zilijonas May 22 '18 at 22:11

2 Answers2

4

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.

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
2

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
Malt
  • 28,965
  • 9
  • 65
  • 105