十进制转十六进制

来源:互联网 发布:淘宝客服处理问题案例 编辑:程序博客网 时间:2024/06/06 02:55
import java.util.Scanner;public class TEST {  public static void main(String[] args) {    Scanner input = new Scanner(System.in);    System.out.print("Enter a decimal number: ");    int decimal = input.nextInt();    System.out.println("The hex number for decimal " +       decimal + " is \n" + decimalToHex(decimal));  }  public static String decimalToHex(int decimal) {    String hex = "";      while (decimal != 0) {      int hexValue = decimal % 16;       hex = toHexChar(hexValue) + hex;      decimal = decimal / 16;    }     return hex;  }  public static char toHexChar(int hexValue) {    if (hexValue <= 9 && hexValue >= 0)      return (char)(hexValue + '0');    else          // hexValue <= 15 && hexValue >= 10      return (char)(hexValue - 10 + 'A');  }}
0 0
原创粉丝点击