📜  Java程序来说明HexaDecimal的用法

📅  最后修改于: 2022-05-13 01:54:27.385000             🧑  作者: Mango

Java程序来说明HexaDecimal的用法

一个十进制数 是数字之和乘以一个 10 的幂。十进制数用基数 10 表示,而十六进制数表示 是数字之和乘以 16 的幂,不知何故,它们的内部工作有相似的特征,只是它们是表示数字的两种不同方式。十六进制系统有 16 个不同的数字符号。使用从 0 到 15 的数字组合可以生成不同的数字。十六进制系统中的表示与从 0 到 9 的十进制数字系统相同,但随后发生了变化。

表示:

插图:内部工作

A. Decimal to Hexadecimal Number System

(1) (13)10 --> (D)16
    Directly can be writen 13 as D in hexadecimal system
     
(2) (16)10 ---> (10)16
    ( 16 )16 = ( 1 x 161) + ( 0 * 160)
  
(3) (59)10 ---> (3B)16
   ( 59 )10 --> ( 3 * 161) + (11 * 160)

结论:

For a Decimal Number system-> ( 421 )10 = (4 x 102) + (2 x 101) + (1 x 100)
B. Similarly ,Hexadecimal to Decimal Number System
   (8A)16 ---> (138)10
    (8A)16 --> (8 x 161) + (10 x 160)

结论:

In Java programs, hexadecimal numbers are written by placing 0x before numbers.

下面是4 个例子来说明十六进制数的用法

  1. 将十六进制数转换为十进制数
  2. 将十进制数转换为十六进制数
  3. 将十六进制数转换为长数
  4. 将 Long 数转换为 Hex 数

示例 1: Java程序将十六进制数转换为十进制数

Java
// Java program to convert Hex number to Decimal number
 
// Importing input/output java library
import java.io.*;
 
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Hexadecimal number stored in a string
        String hexNum = "100";
 
        /* Random hexadecimal number */
 
        // Passing hexnum and base as parameters
        // which is 16 to parseInt function
        int decimal = Integer.parseInt(hexNum, 16);
 
        // Printing the output result as
        // decimal equivalent of hexa-decimal
        System.out.println("Decimal value is " + decimal);
    }
}


Java
// Java Program to Illustrate the Usage of HexaDecimal
 
// Importing input/output java library
import java.io.*;
 
class GFG {
 
    // Main driver function
    public static void main(String[] args)
    {
 
        /* Decimal number to be converted */
        int i = 257;
 
        // Using toHexString() method for getting decNum and
        // Storing the hexaDecNum in a string
        String hex = Integer.toHexString(i);
 
        // Printing hexaDecNum of decNum
        System.out.println("Hex value is " + hex);
    }
}


Java
// Java Program to Illustrate the Usage of HexaDecimal
 
// Importing input/output java library
import java.io.*;
 
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Hexadecimal number stored in a string
        String hexNum = "10000";
 
        // passing hexnum and base as parameters
        // which is 16 to parseLong function
        long num = Long.parseLong(hexNum, 16);
 
        // Printing long value of HexaDecNum
        System.out.println("Long value is " + num);
    }
}


Java
// Java Program to Illustrate the Usage of HexaDecimal
 
// Importing java input/output library
import java.io.*;
 
class GFG {
 
    // Main driver function
    public static void main(String[] args)
    {
 
        /* Long number to be converted */
        long i = 1024;
 
        // Storing the result in a string
        String hex = Long.toHexString(i);
 
        // Displaying Result
        System.out.println("Hex value is " + hex);
    }
}



输出
Decimal value is 256

示例 2: Java程序将十进制数转换为十六进制数 

Java

// Java Program to Illustrate the Usage of HexaDecimal
 
// Importing input/output java library
import java.io.*;
 
class GFG {
 
    // Main driver function
    public static void main(String[] args)
    {
 
        /* Decimal number to be converted */
        int i = 257;
 
        // Using toHexString() method for getting decNum and
        // Storing the hexaDecNum in a string
        String hex = Integer.toHexString(i);
 
        // Printing hexaDecNum of decNum
        System.out.println("Hex value is " + hex);
    }
}


输出
Hex value is 101

示例 3: Java程序将 Hex 数转换为 Long 数

Java

// Java Program to Illustrate the Usage of HexaDecimal
 
// Importing input/output java library
import java.io.*;
 
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Hexadecimal number stored in a string
        String hexNum = "10000";
 
        // passing hexnum and base as parameters
        // which is 16 to parseLong function
        long num = Long.parseLong(hexNum, 16);
 
        // Printing long value of HexaDecNum
        System.out.println("Long value is " + num);
    }
}


输出
Long value is 65536

示例 4: Java程序将 Long 数转换为 Hex 数

Java

// Java Program to Illustrate the Usage of HexaDecimal
 
// Importing java input/output library
import java.io.*;
 
class GFG {
 
    // Main driver function
    public static void main(String[] args)
    {
 
        /* Long number to be converted */
        long i = 1024;
 
        // Storing the result in a string
        String hex = Long.toHexString(i);
 
        // Displaying Result
        System.out.println("Hex value is " + hex);
    }
}


输出
Hex value is 400

注意:十六进制数还有两个约定,400h 或 $400。它们都与 0x400 相同。