Converting a hexadecimal string argument to an integer number returned

 
package com.duapp.itfanr;

//编写一个函数将一个十六进制数的字符串参数转换成整数返回。
public class CharDemo {
	/**
	 * @param args
	 */	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String str = "1a";
		int len = str.length();
		int sum = 0;
		for (int i = 0; i < len; i++) {
			char c = str.charAt(len - 1 - i);
			int n = Character.digit(c, 16);
			sum += n * (1 << (4 * i));
		}
		System.out.print(sum) ;//结果是26
                //System.out.println(Integer.parseInt(str,16)) ;
	}

}
static int digit (char ch, int radix): Returns the current character according to the radix decimal value. If not Character.MIN_RADIX <= radix <= Character.MAX_RADIX, or, ch radix radix is ​​not a valid value returns "-1"; if ch is between "uppercase" A to Z, return ch - ' a '+ value of 10; if "lower case" a between z, return ch -' a '+ value of 10.
System.out.println("Character.MIN_RADIX: " + Character.MIN_RADIX );
System.out.println("Character.MAX_RADIX: " + Character.MAX_RADIX );
System.out.println("Character.digit('2',2): " + Character.digit('2',2) );
System.out.println("Character.digit('7',10): " + Character.digit('7',10) );
System.out.println("Character.digit('F',16): " + Character.digit('F',16) );
//结果为:
Character.MIN_RADIX: 2
Character.MAX_RADIX: 36
Character.digit('2',2): -1
Character.digit('7',10): 7
Character.digit('F',16): 15
I feel that white is a character by the base to decimal. such as:
System.out.println(Character.digit('F',16)) ;//'f'也可以
15. DESCRIPTION output is 'F' in the letters decimal 15. If you enter 'G', since the hexadecimal letter does not exist, it returns -1. Reference: [. 1]. Http://java.chinaitlab.com/base/762366_4.html

Reproduced in: https: //my.oschina.net/itfanr/blog/195712

Guess you like

Origin blog.csdn.net/weixin_33840661/article/details/91799458