10进制转16进制

问题描写叙述
  十六进制数是在程序设计时常常要使用到的一种整数的表示方式。

它有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.io.*;
class Main
{
	public static void main(String[] args)throws Exception 
	{
		BufferedReader bf = new BufferedReader(
			new InputStreamReader(System.in));
		int a = Integer.parseInt(bf.readLine());
		String s = fun(a);
		System.out.println(s);
	}
	public static String fun(int i){
		String s = new String ("0123456789ABCDEF");
		char [] buf = new char[32];
		int charPos=32;
		int radix=1<<4;
		int mask = radix-1;
		do
		{
			buf[--charPos]=s.charAt(i&mask);
			i>>>=4;
		}
		while (i!=0);
		return new String(buf,charPos,(32-charPos));
	}
}
-----------
mport java.io.*;
class Main
{
	final static char[] digits = {
		'0' , '1' , '2' , '3' , '4' , '5' ,
		'6' , '7' , '8' , '9' , 'a' , 'b' ,
		'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
		'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
		'o' , 'p' , 'q' , 'r' , 's' , 't' ,
		'u' , 'v' , 'w' , 'x' , 'y' , 'z'
		};	
	public static void main(String[] args)throws Exception 
	{
		BufferedReader bf = new BufferedReader(
			new InputStreamReader(System.in));
		int a = Integer.parseInt(bf.readLine());
		String s = fun(a).toUpperCase();
		System.out.println(s);
	}
	public static String fun(int i){
		char [] buf = new char[32];
		int charPos=32;
		int radix=1<<4;
		int mask = radix-1;
		do
		{
			buf[--charPos]=digits[i & mask];
			i>>>=4;
		}
		while (i!=0);
		return new String(buf,charPos,(32-charPos));
	}
}


原文地址:https://www.cnblogs.com/blfbuaa/p/7221733.html