Integer.parseInt(string)实际上是如何工作的?

最近被问到这个问题并且不知道答案。 从高层可以解释Java如何获取字符/字符串并将其转换为int。

非常感谢

卡尔

编辑:也很高兴知道其他语言是否也做类似的事情。

通常这样做是这样的:

  • init结果为0
  • 对于字符串中的每个字符执行此操作
    • 结果=结果* 10
    • 从字符中获取数字(’0’是48 ASCII(或0x30),所以只需从字符ASCII代码中减去该数字即可得到数字)
    • 将数字添加到结果中
  • 返回结果

编辑 :这适用于任何基数,如果你用正确的基数替换10并调整从相应字符获得数字(应该适用于低于10的基数,但需要稍微调整更高的基数 – 如hex -因为字母与数字分开7个字符)。

编辑2 :字符到数字值的转换:字符“0”到“9”的ASCII值为48到57(以hex为0x30到0x39),因此为了将字符转换为其数字值,需要进行简单的减法。 通常它是这样完成的(其中ord是给出字符ASCII码的函数):

 digit = ord(char) - ord('0') 

对于更高的数字基数,字母用作’数字’(六进制中的AF),但字母从65(0x41 hexa)开始,这意味着我们必须考虑到的差距:

 digit = ord(char) - ord('0') if digit > 9 then digit -= 7 

示例:’B’为66,因此ord(’B’) – ord(’0’)= 18.由于18大于9,我们减去7,最终结果为11 – ‘digit’B的值,

还有一点需要注意 – 这只适用于大写字母,因此必须先将数字转换为大写。

Java API的源代码是免费提供的。 这是parseInt()方法。 它相当长,因为它必须处理许多特殊和极端情况。

 public static int parseInt(String s, int radix) throws NumberFormatException { if (s == null) { throw new NumberFormatException("null"); } if (radix < Character.MIN_RADIX) { throw new NumberFormatException("radix " + radix + " less than Character.MIN_RADIX"); } if (radix > Character.MAX_RADIX) { throw new NumberFormatException("radix " + radix + " greater than Character.MAX_RADIX"); } int result = 0; boolean negative = false; int i = 0, max = s.length(); int limit; int multmin; int digit; if (max > 0) { if (s.charAt(0) == '-') { negative = true; limit = Integer.MIN_VALUE; i++; } else { limit = -Integer.MAX_VALUE; } multmin = limit / radix; if (i < max) { digit = Character.digit(s.charAt(i++),radix); if (digit < 0) { throw NumberFormatException.forInputString(s); } else { result = -digit; } } while (i < max) { // 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); } if (negative) { if (i > 1) { return result; } else { /* Only got "-" */ throw NumberFormatException.forInputString(s); } } else { return -result; } } 

我不确定你在寻找什么,就像“高水平”一样。 我试试看:

  • 取String,逐个解析所有字符
  • 从总共0开始
  • 如果它在0到9之间,则total = (total x 10) + current
  • 完成后,总计就是结果
 public class StringToInt { public int ConvertStringToInt(String s) throws NumberFormatException { int num =0; for(int i =0; i=48)&&((int)s.charAt(i)<=59)) { num = num*10+ ((int)s.charAt(i)-48); } else { throw new NumberFormatException(); } } return num; } public static void main(String[]args) { StringToInt obj = new StringToInt(); int i = obj.ConvertStringToInt("1234123"); System.out.println(i); } } 
  • 找到字符串的长度(比如maxSize)
  • 初始化结果= 0
  • 开始循环(int j = maxSize,i = 0; j> 0; j–,i ++)
  • int digit = Character.digit(s.charAt(i))
  • 结果=结果+数字*(10次幂j-1)
  • 结束循环
  • 返回结果

这是我对parse int简单实现

 public static int parseInteger(String stringNumber) { int sum=0; int position=1; for (int i = stringNumber.length()-1; i >= 0 ; i--) { int number=stringNumber.charAt(i) - '0'; sum+=number*position; position=position*10; } return sum; } 

这是我想出的(注意:没有对字母进行检查)

 int convertStringtoInt(String number){ int total =0; double multiplier = Math.pow(10, number.length()-1); for(int i=0;i