二进制数用十进制表示----parseInt()方法分析&java7新特性二进制文本

实例:

输入一个二进制数,将其转化为十进制整数。

实现一  代码:

int x = Integer.parseInt(_number, 2);
其中_number表示二进制数,例如:

int x = Integer.parseInt("1100110", 2);
System.out.println(x);    //102

此处用到了parseInt(String s, int radix),常见的是parseInt(String s).查看其源码:

public static int parseInt(String s) throws NumberFormatException {
    return parseInt(s,10);
}

实质上调用的还是parseInt(String s, int radix)函数,查看源码:

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) {		//2
      throw new NumberFormatException("radix " + radix +
            " less than Character.MIN_RADIX");
   }

   if (radix > Character.MAX_RADIX) {		//36
      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;	//0x7fffffff = -2147483647

   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 != '+')
            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;
}
即就是本函数可以实现从2到36进制数的转化。例如源码给的例子

 parseInt("0", 10) returns 0
 parseInt("473", 10) returns 473
 parseInt("+42", 10) returns 42
 parseInt("-0", 10) returns 0
 parseInt("-FF", 16) returns -255
 parseInt("1100110", 2) returns 102
 parseInt("2147483647", 10) returns 2147483647
 parseInt("-2147483648", 10) returns -2147483648
 parseInt("2147483648", 10) throws a NumberFormatException
 parseInt("99", 8) throws a NumberFormatException
 parseInt("Kona", 10) throws a NumberFormatException
 parseInt("Kona", 27) returns 411787

实现二 代码:

int num = 0b1100110;
System.out.println(num);	//102

这是java7的新特性,即数字常量可以用二进制文本表示。

而且在使用整型常量时可以用下划线进行分隔表示,提高可读性。例如:

System.out.println(0b101_1100_0011_0010_1011__1010_0011);	//96676771

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值