JAVA hex to decimal
- package firstjava;
- import java.util.Scanner;
- public class Hex {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- String sIn = input.nextLine();
- int iResult;
- iResult = HexToDec(sIn);
- System.out.println(iResult);
- }
- public static int HexToDec(String sHex){
- int i,j,iResu;
- char hexCar;
- int iMul;
- int iLength = sHex.length();
- iResu = 0;
- iMul = j = 1;
- for ( i=iLength-1 ; i>=0 ;i-- )
- {
- if ( sHex.charAt(i) >= 'A' && sHex.charAt(i) <= 'F')
- j = sHex.charAt(i)-'A' + 10;
- if ( sHex.charAt(i) >= '0' && sHex.charAt(i) <= '9')
- j = sHex.charAt(i) -'0';
- iResu = iResu + j * iMul;
- iMul = iMul*16;
- }
- return iResu;
- }
- }
留言
張貼留言