📌  相关文章
📜  Java程序将十六进制转换为二进制

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

Java程序将十六进制转换为二进制

十六进制是一种非常有用的数字系统,其前提是一次将 4 个比特组合在一起构成系统的单个实体,该系统由 16 个符号组成,其中包括0-9的 10 个数字和来自AF的前六个字母. 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 hashMap
            = new HashMap();
 
        // 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));
    }
}


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


输出
DEAFA
11011110101011111010

方法二:

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

  1. 首先,我们编写函数将十进制转换为二进制。
  2. 接下来,我们编写将十六进制转换为十进制的函数,并在该函数中调用上述函数将转换后的十进制进一步转换为二进制。
  3. 在这个函数中,我们遍历十六进制字符串的长度并一次提取一个字符。
  4. 接下来,我们检查提取的字符是否在 0-9 或 AF 范围内。
  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