Question

public static byte[][] keyArray = new byte[4][4]; 
String hex = "93";
String hexInBinary = Integer.toBinaryString(Integer.parseInt(hex, 16));
keyArray[row][col] = Byte.parseByte(hexInBinary,2); //this line causes the error

This is the error message I get,

"Exception in thread "main" java.lang.NumberFormatException: Value out of range. Value:"10010011" Radix:2."

I don't want to use getBytes(), because I actually have a long string, "0A935D11496532BC1004865ABDCA42950." I want to read 2 hex at a time and convert to byte.

EDIT:

how I fixed it:

String hexInBinary = String.format("%8s", Integer.toBinaryString(Integer.parseInt(hex, 16))).replace(' ', '0');
keyArray[row][col] = (byte)Integer.parseInt(hexInBinary, 2);
Was it helpful?

Solution

As it is written in the exception message the string you are trying to convert to byte exceeds the max. value of a byte can have.

In your example the string "10010011" equals to 147, but the max value for a byte variable is 2^7 - 1 = 127.

You might want to check the Byte Class documentation; http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Byte.html#MAX_VALUE

So i suggest to use Integer.parseInt(), instead of parseByte method and then cast the int value to byte, integer value 147 will become -109 when you cast it to byte value.

OTHER TIPS

public class ByteConvert {
    public static void main(String[] argv) {
        String hex = "93";
        String hexInBinary = Integer.toBinaryString(Integer.parseInt(hex, 16));
        int intResult = Integer.parseInt(hexInBinary,2);
        System.out.println("intResult = " + intResult);
        byte byteResult = (byte) (Integer.parseInt(hexInBinary,2));
        System.out.println("byteResult = " + byteResult);
        byte result = Byte.parseByte(hexInBinary,2);
        System.out.println("result = " + result);
    }
}

C:\JavaTools>java ByteConvert
intResult = 147
byteResult = -109
Exception in thread "main" java.lang.NumberFormatException: Value out of range.
Value:"10010011" Radix:2
        at java.lang.Byte.parseByte(Unknown Source)
        at ByteConvert.main(ByteConvert.java:9)

As can be seen, parseByte detects a value "larger" than a byte.

According to the java documention at http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Byte.html#parseByte(java.lang.String),

 "The characters in the string must all be digits, of the specified radix 
 (as determined by whether Character.digit(char, int) returns a nonnegative
 value) except that the first character may be an ASCII minus 
 sign '-' ('\u002D') to indicate a negative value."

So the binary representation is not twos-compliment. The byte 10010011 in twos-compliment notation would be negative given that the upper bit is a 1. So if you want to get the same value as the twos-compliment byte 10010011, you would need to do Byte.parseByte("-1101100");.

Put another way, the maximum value an unsigned byte can have is 255. The maximum value a signed byte can have is 127 (because if the most-significant-bit is 1, then it is interpreted as a negative number). However, the issue with parseByte() is that it instead uses an explicit "-" symbol instead of a 1 to indicate negative numbers. This means that the rest of the "byte" can only use 7 bits.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top