基础练习 十六进制转十进制

来源:互联网 发布:mysql syntax 编辑:程序博客网 时间:2024/06/06 19:44
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner out=new Scanner(System.in);while(out.hasNext()){String str=out.nextLine();String hex=new StringBuilder(str).reverse().toString();int t=0;long result = 0;while(t<hex.length()){if('A'<=hex.charAt(t)&&hex.charAt(t)<='F'){int b=comp(hex.charAt(t));result+=b*(long)Math.pow(16, t++);}else{int temp=hex.charAt(t)-'0';result+=temp*(long)Math.pow(16, t++);}}System.out.println(result);}}private static int comp(char charAt) {// TODO Auto-generated method stubint a[]=new int[100];int k=65,i=10;while(i<16) a[k++]=i++;return a[charAt];}}

直接调用

import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner out=new Scanner(System.in);while(out.hasNext()){String str=out.nextLine();System.out.println(Long.valueOf(str,16).toString());}}}


原创粉丝点击