package com.arithmetic;
public class A {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
System.out.println(atoi("5234"));
Integer.parseInt("42");
System.out.println(Integer.MIN_VALUE);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(itoa(23214));
}
//将字符串转换成整型数
public static int atoi(String str) throws Exception {
boolean negative = false;
int value = 0;
if (str == null || "".equals(str)) {
throw new Exception("null String or the string has no character");
}
for (int i = 0; i < str.length(); i++) {
if (i == 0 && (str.charAt(0) == '-' || str.charAt(0) == '+')) {
if (str.charAt(0) == '-') {
negative = true;
}
} else {
if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {
value = value * 10 + (str.charAt(i) - '0');
if (value > Integer.MAX_VALUE) {
throw new Exception("Out of integer range");
} else if (value < Integer.MIN_VALUE) {
throw new Exception("Out of integer range");
}
} else {
throw new NumberFormatException("not an integer");
}
}
}
return negative ? value * -1 : value;
}
//将字符串转换成整型数,Java 源代码
public static int parseInt(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) {
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, len = s.length();
int limit = -Integer.MAX_VALUE;
int multmin;
int digit;
if (len > 0) {
char firstChar = s.charAt(0);
if (firstChar < '0') { // Possible leading "+" or "-"
if (firstChar == '-') {
negative = true;
limit = Integer.MIN_VALUE;
} else if (firstChar != '+')
new NumberFormatException("For input string: \"" + s + "\"");
if (len == 1) // Cannot have lone "+" or "-"
new NumberFormatException("For input string: \"" + 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) {
new NumberFormatException("For input string: \"" + s + "\"");
}
if (result < multmin) {
new NumberFormatException("For input string: \"" + s + "\"");
}
result *= radix;
if (result < limit + digit) {
new NumberFormatException("For input string: \"" + s + "\"");
}
result -= digit;
}
} else {
new NumberFormatException("For input string: \"" + s + "\"");
}
return negative ? result : -result;
}
public static NumberFormatException forInputString(String s) {
return new NumberFormatException("For input string: \"" + s + "\"");
}
// 把一整数转换为字符串
public static String itoa(int number, char[] s) {
int i = 0, j, sign;
StringBuffer sb = new StringBuffer();
if ((sign = number) < 0) { // 记录符号
number = -number; // 使n成为正数
}
do {
s[i++] = (char) (number % 10 + '0'); // 取下一个数字
} while ((number /= 10) > 0);
// 删除该数字
// 取负号
if (sign < 0) {
s[i++] = '-';
}
s[i] = '\0';
for (j = i; j >= 0; j--) {
// 生成的数字是逆序的,所以要逆序输出
sb.append(s[j]);
}
return sb.toString();
}
// 把一整数转换为字符串
public static String itoa(int number) {
int sign;
StringBuffer sb = new StringBuffer();
if ((sign = number) < 0) { // 记录符号
number = -number; // 使n成为正数
}
do {
sb.append((number % 10)); // 取下一个数字
} while ((number /= 10) > 0);
// 删除该数字
// 取负号
if (sign < 0) {
sb.append('-');
}
// 生成的数字是逆序的,所以要逆序输出
return sb.reverse().toString();
}
}