进制之间的转换

进制转换

代码如下:

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');
}
}
}

希望有所帮助

只相信苦尽甘来
原文地址:https://www.cnblogs.com/F001li/p/7055712.html