Java – numberformatexception when converting from string to byte

Something strange happened to me I have a binary number in string form, especially "01001100" But I get the above exception by executing the following code

String s = "01001100";
byte b = Byte.parseByte(s);

But why? We can store the maximum number in a byte Up to 127 minutes and up to - 128 And the decimal equivalent of the above numbers is 76, which is completely within this range

The special exceptions I get are:

java. Lang.numberformatexception: value out of range Value: 01001100 base: 10

Is there any way to get rid of it Yes, I have to use bytes because I just extract the data stored in the image byte by byte thank you.

Solution

The key is at the end of the exception string: radius: 10 You are converting the decimal value 1001100 to one byte, but it is not suitable Try this:

String s = "01001100";
byte b = Byte.parseByte(s,2);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>