java十进制转换16进制

import java.util.*;
public class ten_sixteen {
	public static void main(String[] args) {
		Scanner input =new Scanner(System.in);
		System.out.print("enter a number: ");
		int decimal=input.nextInt();
		System.out.println(decimal+ " is " +decimaltohex(decimal));
	}
	public static String decimaltohex(int decimal) {
		String hex="";
		while(decimal!=0) {
			int hexvalue=decimal%16;
			hex=tohexchar(hexvalue)+hex;
			decimal/=16;
		}
		return hex;
	}
	public static char tohexchar(int hexvalue) {
		if(hexvalue<=9&&hexvalue>=0)
			return (char)(hexvalue+'0');
		else
			return (char)(hexvalue-10+'A');
	}
}


猜你喜欢

转载自blog.csdn.net/j_linlian/article/details/79671898