///二进制对应的字符串String s = "10101000" ///赫夫曼编码字节数组:return : 168 ///经过强转 (byte)168 --> 得到 -88 ///我们平时用到Integer.parseInt("123");其实默认是调用了int i =Integer.parseInt("123",10); 其中10代表的默认是10进制 publicstaticintparseInt(String s, int radix)throws NumberFormatException { /* * WARNING: This method may be invoked early during VM initialization * before IntegerCache is initialized. Care must be taken to not use * the valueOf method. */
if (s == null) { thrownewNumberFormatException("null"); } ///public static final int MIN_RADIX = 2; if (radix < Character.MIN_RADIX) { thrownewNumberFormatException("radix " + radix + " less than Character.MIN_RADIX"); }
///public static final int MAX_RADIX = 36; if (radix > Character.MAX_RADIX) { thrownewNumberFormatException("radix " + radix + " greater than Character.MAX_RADIX"); }
intresult=0; booleannegative=false; inti=0, len = s.length(); intlimit= -Integer.MAX_VALUE; int multmin; int digit;
if (len > 0) { charfirstChar= s.charAt(0); if (firstChar < '0') { // Possible leading "+" or "-" if (firstChar == '-') { negative = true; limit = Integer.MIN_VALUE; } elseif (firstChar != '+') throw NumberFormatException.forInputString(s);
if (len == 1) // Cannot have lone "+" or "-" throw NumberFormatException.forInputString(s); i++; } multmin = limit / radix; while (i < len) { // Accumulating negatively avoids surprises near MAX_VALUE digit = Character.digit(s.charAt(i++),radix); if (digit < 0) { throw NumberFormatException.forInputString(s); } if (result < multmin) { throw NumberFormatException.forInputString(s); } result *= radix; if (result < limit + digit) { throw NumberFormatException.forInputString(s); } result -= digit; } } else { throw NumberFormatException.forInputString(s); } return negative ? result : -result; }