0

I am trying to parse the following String to Byte.But it gives me NumberFormat Exception.Can some body tell me what is the solution for this?

Byte.parseByte("11111111111111111111111110000001", 2);
6
  • 2
    Thats way larger than a byte. Commented Feb 18, 2013 at 13:08
  • A byte is made of 8 bits... each of your 0/1 chars is a bit. Commented Feb 18, 2013 at 13:10
  • DV - check javadocs before posting question. Commented Feb 18, 2013 at 13:13
  • and here's a dup. stackoverflow.com/questions/6996707/java-byte-parsebyte-error Commented Feb 18, 2013 at 13:14
  • If you still want to ignore overflows your option is to parse as int and then cast int to byte. Commented Feb 18, 2013 at 13:14

4 Answers 4

2

Byte.parseByte() handles binary string as sign-magnitude not as a 2's complement, so the longest length you can have for a byte is 7 bits with a sign.

In other words, to represent -127, you should use:

Byte.parseByte("-111111", 2);

The following throws NumberFormatException:

Byte.parseByte("10000000", 2);

However, the binary literal of -127 is:

byte b = (byte) 0b10000000;

The same behavior is applied to the other parseXXX() methods.

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

Comments

2

Out of range of byte ie -128 to 127. From parseByte(String s,int radix) javadoc:

public static byte parseByte(String s, int radix)throws NumberFormatException

Parses the string argument as a signed byte in the radix specified by the second argument. 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. The resulting byte value is returned. An exception of type NumberFormatException is thrown if any of the following situations occurs:

  1. The first argument is null or is a string of length zero.
  2. The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX.
  3. Any character of the string is not a digit of the specified radix, except that the first character may be a minus sign '-' ('\u002D') provided that the string is longer than length 1.
  4. The value represented by the string is not a value of type byte.

Returns: the byte value represented by the string argument in the specified radix Throws: NumberFormatException - If the string does not contain a parsable byte.

4 Comments

This is not correct. parseXXX() methods handle binary string as sign-magnitude not as a 2's complement. See my answer.
@Eng.Fouad: The answer here is correct, actually it's just copy/paste from javadoc without a link to it.
@jlordo I know and that is why I did not down-vote this answer. But they said Parses the string argument as a signed byte which IMHO is not quite true if radix = 2.
@Eng.Fouad: Yes, it's true. Java doesn't know unsigned bytes, so they say it parses the string argument as a signed byte. They nowhere say the first digit is the sign bit.
1

from javadocs

An exception of type NumberFormatException is thrown if any of the following situations occurs:

  • The first argument is null or is a string of length zero.
  • The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX.
  • Any character of the string is not a digit of the specified radix, except that the first character may be a minus sign '-' ('\u002D') provided that the string is longer than length 1.
  • The value represented by the string is not a value of type byte.

Your value is the second case that is out of range -128 to 127

Comments

0

Value is too large to be parsed in byte Try this:

new BigInteger("011111111111111111111111110000001", 2).longValue();

Comments

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.