简体   繁体   English

将字符串解析为字节的NumberFormat异常?

[英]NumberFormat Exception to parse String to Byte?

我正在尝试将以下字符串解析为Byte。但是它给了我NumberFormat Exception.body可以告诉我对此的解决方案是什么?

Byte.parseByte("11111111111111111111111110000001", 2);

Out of range of byte ie -128 to 127. From parseByte(String s,int radix) javadoc: 超出字节范围,即-128到127。来自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 '-' ('\-') to indicate a negative value. 字符串中的字符必须全部为指定基数的数字(由Character.digit(char,int)是否返回非负值来确定),除了第一个字符可以是ASCII减号'-'('\\ u002D ')表示负值。 The resulting byte value is returned. 返回结果字节值。 An exception of type NumberFormatException is thrown if any of the following situations occurs: 如果发生以下任一情况,将引发NumberFormatException类型的异常:

  1. The first argument is null or is a string of length zero. 第一个参数为null或长度为零的字符串。
  2. The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX. 基数小于Character.MIN_RADIX或大于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 '-' ('\-') provided that the string is longer than length 1. 字符串的任何字符都不是指定基数的数字,除非第一个字符可以是减号'-'('\\ u002D'),前提是该字符串长于长度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. 返回:由指定基数中的string参数表示的字节值抛出:NumberFormatException-如果字符串不包含可分析的字节。

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. Byte.parseByte()二进制字符串作为符号大小而不是2的补码来处理,因此一个字节的最大长度是7个带符号的位。

In other words, to represent -127 , you should use: 换句话说,要表示-127 ,应使用:

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

The following throws NumberFormatException : 以下引发NumberFormatException

Byte.parseByte("10000000", 2);

However, the binary literal of -127 is: 但是,-127的二进制文字是:

byte b = (byte) 0b10000000;

The same behavior is applied to the other parseXXX() methods. 相同的行为适用于其他parseXXX()方法。

from javadocs 来自javadocs

An exception of type NumberFormatException is thrown if any of the following situations occurs: 如果发生以下任一情况,将引发NumberFormatException类型的异常:

  • The first argument is null or is a string of length zero. 第一个参数为null或长度为零的字符串。
  • The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX. 基数小于Character.MIN_RADIX或大于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 '-' ('\-') provided that the string is longer than length 1. 字符串的任何字符都不是指定基数的数字,除非第一个字符可以是减号'-'('\\ u002D'),前提是该字符串长于长度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 您的值是第二个超出-128到127范围的情况

值太大,无法以字节为单位进行解析请尝试以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM