首页 > 其他 > 详细

进制之间的转换

时间:2017-06-20 19:08:51      阅读:316      评论:0      收藏:0      [点我收藏+]

进制转换

代码如下:

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

 

希望有所帮助

进制之间的转换

原文:http://www.cnblogs.com/F001li/p/7055712.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!