1
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);
6
  • stackoverflow.com/questions/140131/… Commented Oct 30, 2013 at 0:19
  • A byte is a signed value -128 to 127. Commented Oct 30, 2013 at 0:25
  • @arshajii: Byte.parseByte("10010011", 2) fails for me with the exception he posts (Java 7). Commented Oct 30, 2013 at 0:26
  • @vanza The question has since been updated to something that does produce an exception. I'll remove my close vote. Commented Oct 30, 2013 at 0:27
  • Thank you everyone! I fixed it!!!! Integer.parseInt() fixed the problem!! Commented Oct 30, 2013 at 0:41

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

10 Comments

That doesn't really answer the question. "10010011" has only 8 bits and is a valid binary representation of a byte (-109 in two's complement, which is what the JLS specifies, to be precise). It's weird the API refuses to parse it.
How come it does not answer the question? I am explaining the reason of the error and suggesting him a solution which works fine. I agree that it is weird that API refuses it, but we can not do anything about that right now, we have to find another solution if it does not work.
It doesn't because "10010011" is a valid byte. Any 8 bits are a valid byte. You're just providing a work around.
The first line of this answer is incorrect. The maximum value a 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.
See link I posted in comment under the question itself. Seems that the behavior is intentionally inconsistent with the language spec. :-/
|
1
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.

Comments

0

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.

1 Comment

For some fun: Integer.parseInt(Integer.toBinaryString(-1), 2). May be consistent with documentation, but fails the POLA.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.