zoukankan      html  css  js  c++  java
  • 进制之间的转换

    进制转换

    代码如下:

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

    希望有所帮助

    只相信苦尽甘来
  • 相关阅读:
    Android随笔
    Android随笔
    Android随笔
    Android随笔
    Android随笔
    Android随笔
    Android随笔
    Android随笔
    Android随笔
    Codeforces Round #551题解
  • 原文地址:https://www.cnblogs.com/F001li/p/7055712.html
Copyright © 2011-2022 走看看