zoukankan      html  css  js  c++  java
  • 16进制转化为10进制

    输入:0xA
    输出:10
    public  static void main(String[] args){
            Scanner scanner = new Scanner(System.in);
            String hex = scanner.next();
            if( hex == null || hex.equals("") || !hex.substring(0,2).equalsIgnoreCase("0x")){
                return;
            }
    
    
            Map<String,Integer> map = new LinkedHashMap<>();
            char ch = 'A';
            int n = 10;
            while(n<16){
                map.put(ch+"",n);
                ch++;
                n++;
            }
    
            long sum = 0L;
            String num = hex.substring(2, hex.length());
            String gol = new String();
            for(int j=num.length()-1; 0<=j; j--){
                char temp = num.charAt(j);
                gol += temp;
    
            }
            for(int i=0; i<gol.length(); i++){
                String k = gol.charAt(i)+"";
                if (map.containsKey(k)) {
                    Integer val = map.get(k);
                    sum += val*Math.pow(16,i);
                    continue;
                }
    
                try{
                    long l = Long.parseLong(k);
                    sum += l*Math.pow(16,i);
                }catch (Exception e){
                    System.out.println("输入有误");
                }
    
            }
    
            System.out.println(sum);
        }

    还有直接用parse*方法更加简单:

    public class SixteenToTen {
        private  Scanner scanner = new Scanner(System.in);
    
        private String testStr;
    
        @Before
        public void initStr(){
            testStr = "0xA1A";
        }
        @Test
        public void scanner(){
            if(testStr == null || testStr.equals("") || !testStr.substring(0,2).equalsIgnoreCase("0x")){
                return ;
            }
    
            String num = testStr.substring(2);
    
            int numInt = Integer.parseInt(num, 16);
            System.out.println(numInt);
    
        }
    }
  • 相关阅读:
    组件库设计
    kill 3000
    nextjs服务端渲染原理
    Web交互增强
    webpack4.0打包的时候一些技巧
    把网站部署到阿里云上的步骤
    typescript使用小结
    webpack 4.0尝鲜
    基于Quick-cocos2d-x的资源更新方案 二
    Android APK是否需要预解压
  • 原文地址:https://www.cnblogs.com/theRhyme/p/9116871.html
Copyright © 2011-2022 走看看