當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。