Java itoa,atoi

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();
	}
}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值