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?
8
and9
(or1000
and1001
). – Elliott Frisch