2016蓝桥杯假期任务之《十进制转十六进制》

问题描述   十六进制数是在程序设计时经常要使用到的一种整数的表示方式。它有0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F共16个符号,分别表示十进制数的0至15。十六进制的计数方法是满16进1,所以十进制数16在十六进制中是10,而十进制的17在十六进制中是11,以此类推,十进制的30在十六进制中是1E。

  给出一个非负整数,将它表示成十六进制的形式。 输入格式   输入包含一个非负整数a,表示要转换的数。0<=a<=2147483647 输出格式   输出这个整数的16进制表示 样例输入 30 样例输出 1E 代码如下:

import java.util.Scanner;  
  
public class Main {  
    public static void main(String[] args) throws Exception {  
        Scanner input = new Scanner(System.in);  
        long a = input.nextLong();  
        char[] s = new char[10000001];  
        int i = 0;  
        if(a==0)  
            System.out.println(0);  
        while (a != 0) {  
            int t = (int)(a%16);  
            if (a % 16 >= 0 && a % 16 <= 9){  
                s[i++] = (char)('0'+t);  
            }  
            else {  
                if(a%16>=10&&a%16<=15)  
                    s[i++]=(char)('A'+t-10);  
            }  
            a/=16;  
        }  
        for (int j = i-1; j>=0; j--)  
            System.out.print(s[j]);  
    }  
}  

运行结果:

30
1E

    原文作者:进制转换
    原文地址: https://blog.csdn.net/Liuchang54/article/details/50864286
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞