• 写出一个程序,接受一个十六进制的数,输出该数值的十进制表示。(多组同时输入 )


    package com.pagination.plus.workTrain;
    
    
    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class Main0 {
        public static void main(String[] args) throws IOException {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream("D:\JavaData\tmp/input.txt")));
            //BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            String read = null;
            //String[] hexTable = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
            String hexTable = "0123456789ABCDEF";
            while ((read=bufferedReader.readLine())!=null){
                String hexNum = read.substring(2).toUpperCase();
                int num=0;
                for(int j=0, i=hexNum.length()-1;i>=0;i--,j++){
                    num += hexTable.indexOf(hexNum.charAt(j)) * (int)Math.pow(16,i);
                }
                System.out.println(num);
          		//System.out.println(Integer.parseInt(hexNum,16));
            }
        }
    
    
    }
    
    

    如果对api熟悉,直接使用java提供的可以一行搞定
    这里需要记下parse的用法,Integer,Long,Double等包装类都提供了相应的方法,如果不涉及其他进制的转换,只是字符串样式的十进制数,直接使用valueOf就OK。
    static Integer valueOf(String s)
    Returns an Integer object holding the value of the specified String.

    System.out.println(Integer.parseInt(hexNum,16));
    

    注意:如果使用了parse,一定要注意转换数据的范围,
    Integer.parseInt需要注意的最小值为
    MIN_VALUE = 0x80000000;(负数在计算机中都是保存的补码)
    最大值为
    MAX_VALUE = 0x7fffffff;
    一定要保证在这个数据间才行,否则会出现java.lang.NumberFormatException

  • 相关阅读:
    循环神经网络(Recurrent Neural Network)
    特征选择
    程序猿能挣多少钱
    python socket
    python 2 encode and decode
    pandas series
    source collection list
    pep8摘要
    python 正则表达式
    django显示图片
  • 原文地址:https://www.cnblogs.com/InternetJava/p/12543189.html
Copyright © 2020-2023  润新知