当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Hexadecimal转Binary用法及代码示例


十六进制是一种非常有用的数字系统,它基于一次将 4 位组合在一起构成系统的单个实体,该系统由 16 个符号组成,包括 0-9 的 10 位数字和 A-F 的前六个字母。 Hexadecimal 一词源自 Hex 一词,意思是六,十进制,意思是十。

因此,组合词表示十六,即六和十相加。十六进制序列也称为基数或基数 16。在处理不同的数字系统时,能够将它们从一个系统转换为另一个系统变得至关重要。在本文中,我们将重点介绍将十六进制转换为二进制,这是一个由 1 和 0 组成的系统,是计算机存储和处理指令和数据的机制。

例子:

Hexadecimal Sequence:458A
Binary Equivalent:0100010110001010
Explanation:Binary representation of A:1010
          Binary representation of 8:1000
          Binary representation of 5:0101
          Binary representation of 4:0100

Hexadecimal Sequence:B36
Binary Equivalent:101100110110

将十六进制转换为二进制有两种方法,具体如下:



  1. 使用键值对进行从十六进制字符到其等效二进制的相应转换
  2. 将十六进制转换为其等效的十进制,然后再转换为其等效的二进制

方法一:

使用这种方法,我们制定键值并提取十六进制字符串的每个字符,添加其对应的二进制序列并返回完整的二进制序列。

  1. 创建一个 HashMap 来存储键值对。
  2. 接受十六进制序列作为字符串,并在遍历字符串长度时提取每个字符。
  3. 检查提取的字符是否存在于 HashMap 的键中。
  4. 如果存在,则将存储二进制序列的字符串与键的相应值连接起来。
  5. 如果不存在,则返回无效的十六进制字符串。

码:

Java


// Java program to convert Hexadecimal to Binary
import java.util.HashMap;
class GFG {
    // declaring the method to convert
    // Hexadecimal to Binary
    String hexToBinary(String hex)
    {
        // variable to store the converted
        // Binary Sequence
        String binary = "";
        // converting the accepted Hexadecimal
        // string to upper case
        hex = hex.toUpperCase();
        // initializing the HashMap class
        HashMap<Character, String> hashMap
            = new HashMap<Character, String>();
        // storing the key value pairs
        hashMap.put('0', "0000");
        hashMap.put('1', "0001");
        hashMap.put('2', "0010");
        hashMap.put('3', "0011");
        hashMap.put('4', "0100");
        hashMap.put('5', "0101");
        hashMap.put('6', "0110");
        hashMap.put('7', "0111");
        hashMap.put('8', "1000");
        hashMap.put('9', "1001");
        hashMap.put('A', "1010");
        hashMap.put('B', "1011");
        hashMap.put('C', "1100");
        hashMap.put('D', "1101");
        hashMap.put('E', "1110");
        hashMap.put('F', "1111");
        int i;
        char ch;
        // loop to iterate through the length
        // of the Hexadecimal String
        for (i = 0; i < hex.length(); i++) {
            // extracting each character
            ch = hex.charAt(i);
            // checking if the character is
            // present in the keys
            if (hashMap.containsKey(ch))
                // adding to the Binary Sequence
                // the corresponding value of
                // the key
                binary += hashMap.get(ch);
            // returning Invalid Hexadecimal
            // String if the character is
            // not present in the keys
            else {
                binary = "Invalid Hexadecimal String";
                return binary;
            }
        }
        // returning the converted Binary
        return binary;
    }
    // Driver Code
    public static void main(String[] args)
    {
        // instantiating the class
        GFG ob = new GFG();
       
        String hex = "deafa";
       
        System.out.println(hex.toUpperCase());
       
        // printing and calling the
        // hexToBinary() function
        System.out.println(ob.hexToBinary(hex));
    }
}
输出
DEAFA
11011110101011111010

方法二:

这种方法首先将十六进制字符串转换为其十进制等效值,然后再将其转换为其二进制等效值。我们使用两个函数,第一个将十六进制转换为十进制,第二个将十进制转换为二进制。

  1. 首先,我们编写将十进制转换为二进制的函数。
  2. 接下来,我们编写将十六进制转换为十进制的函数,并在该函数内部调用上述函数,将转换后的十进制进一步转换为二进制。
  3. 在这个函数中,我们遍历十六进制字符串的长度并一次提取每个字符。
  4. 接下来,我们检查提取的字符是否在 0-9 或 A-F 范围内。
  5. 如果字符存在于上述范围内,我们将它们连接到十进制字符串。
  6. 接下来,使用 decimalToBinary() 函数将十进制字符串转换为二进制。
  7. 如果在上述范围内没有一个字符,则返回无效的十六进制字符串作为输出。

码:

Java


// Java program to convert Hexadecimal to Binary
class GFG {
    // method to convert Decimal to Binary
    String decimalToBinary(int decimal)
    {
        // variable to store the converted
        // binary string
        String binaryString = "";
        // loop to generate the binary
        while (decimal != 0) {
            // concatenating the remainder
            // on dividing by 2 to the
            // binary string
            binaryString = (decimal % 2) + binaryString;
            // updating the decimal integer
            // by dividing by 2 in each iteration
            decimal /= 2;
        }
        // loop to ensure that each
        // Hexadecimal character is
        // represented by 4 bits
        while (binaryString.length() % 4 != 0) {
            // adding leading 0's if the
            // character is represented by less
            // than 4 bits
            binaryString = "0" + binaryString;
        }
        // returning the converted binary string
        return binaryString;
    }
    // method to convert Hexadecimal to Binary
    String hexToBinary(String hexadecimal)
    {
        // declaring the variables
        int i;
        char ch;
        String binary = "";
        int returnedBinary;
        // converting the accepted Hexadecimal
        // String to upper case
        hexadecimal = hexadecimal.toUpperCase();
        // loop to iterate through the length
        // of the Hexadecimal String
        for (i = 0; i < hexadecimal.length(); i++) {
            // extracting the characters
            ch = hexadecimal.charAt(i);
            // condition to check if
            // the character is not a valid Hexadecimal
            // character
            if (Character.isDigit(ch) == false
                && ((int)ch >= 65 && (int)ch <= 70)
                       == false) {
                // returning Invalid Hexadecimal
                // String for the invalid Hexadecimal
                // character
                binary = "Invalid Hexadecimal String";
                return binary;
            }
            // checking if the character is a valid
            // Hexadecimal alphabet
            else if ((int)ch >= 65 && (int)ch <= 70)
                // converting alphabet to
                // corresponding value such as 10
                // for A and so on using ASCII code
                returnedBinary = (int)ch - 55;
            else
                returnedBinary
                    = Integer.parseInt(String.valueOf(ch));
            // converting the decimal to binary
            // by calling the decimalToBinary() method
            binary += decimalToBinary(returnedBinary);
        }
        // returning the converted binary sequence
        return binary;
    }
    // Driver Code
    public static void main(String[] args)
    {
        // instantiating the class
        GFG ob = new GFG();
         
        String hex = "abcfde";
       
        System.out.println(hex);
       
        // printing and calling the
        // hexToBinary() function to display the
        // output
        System.out.println(ob.hexToBinary(hex));
    }
}
输出
abcfde
101010111100111111011110




相关用法


注:本文由纯净天空筛选整理自ag01harshit大神的英文原创作品 Java Program to Convert Hexadecimal to Binary。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。