JAVA hex to decimal

  1. package firstjava;
  2. import java.util.Scanner;
  3. public class Hex {
  4.     public static void main(String[] args) {
  5.         Scanner input = new Scanner(System.in);
  6.         String sIn = input.nextLine();
  7.         int iResult;
  8.         iResult = HexToDec(sIn);
  9.         System.out.println(iResult);
  10.     }
  11.     public static int HexToDec(String sHex){
  12.     int i,j,iResu;
  13.     char hexCar;
  14.     int iMul;
  15.     int iLength = sHex.length();
  16.     iResu  = 0;
  17.     iMul  = j = 1;
  18.     for ( i=iLength-1 ; i>=0 ;i-- )
  19.         {
  20.         if ( sHex.charAt(i) >= 'A' && sHex.charAt(i) <= 'F')
  21.            j = sHex.charAt(i)-'A' + 10;
  22.         if ( sHex.charAt(i) >= '0' && sHex.charAt(i) <= '9')
  23.             j = sHex.charAt(i) -'0';
  24.         iResu = iResu +  j * iMul;
  25.         iMul = iMul*16;
  26.         }
  27.     return iResu;
  28.     }
  29. }

留言

這個網誌中的熱門文章

Codeforces --- string task

Uva 674 ---- coin change

codeforces 271A --- beautiful year