• Post Reply Bookmark Topic Watch Topic
  • New Topic

why am I getting NumberFormatException with Byte.parseByte()?  RSS feed

 
Bob Peterson
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to convert a long or int to a byte array. I'm restricted to using Java 1.3. I'm first trying to use Long.toBinaryString(myLong), and then pass 8-character substrings into Byte.parseByte(mySubstring, 2), but I'm getting errors like:

java.lang.NumberFormatException: Value out of range. Value:"11110100" Radix:2

What am I doing wrong? A byte is 8-bit, two's complement right? So shouldn't it be able to handle the above string?
 
Daniel Mayer
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A byte is signed, it can contain values between -128 and +127. "10000000" is probably interpreted as +128, though.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!