进制之间的转换

进制转换

代码如下:

package ClassDemo;

import java.util.Scanner;

public class Decimal2Hex {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);

System.out.println("Enter a decimal number: ");
int decimal = input.nextInt();
System.out.print(decimalToHex(decimal));
} private static String decimalToHex(int decimal) {
String hexStr = "";
while (decimal != 0) {
int hexValue = decimal % 16;
hexStr = toHexChar(hexValue) + hexStr;
decimal /= 16;
}
return hexStr;
} private static char toHexChar(int hexValue) {
if (hexValue <= 9 && hexValue >= 0) {
return (char) (hexValue + '0');
} else {
return (char) (hexValue - 10 + 'A');
}
}
}

 

希望有所帮助

posted @ 2017-06-20 18:36  辰峰  阅读(140)  评论(0编辑  收藏  举报