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


Java Octal轉Binary用法及代碼示例


給定一個八進製數作為輸入,任務是將該數字轉換為其等效的二進製數。

例:

Input: Octal Number = 513
Output: Binary equivalent value is:101001011

Explanation: 
Binary equivalent value of 5:101
Binary equivalent value of 1:001

Binary equivalent value of 3:011

八進製數係統:

八進製數字係統是一種位置數字係統,其基數或基數為 8,並使用 0 到 7 八個不同的數字。

二進製數係統:

二進製數是以 2 為底的二進製數字係統表示的數字,它僅使用兩個符號:0 和 1。

八進製到二進製轉換表:

八進製二進製
0000
1001
2010
3011
4100
5101
6110
7111

方法一:樸素的方法

在這種方法中,八進製數的輸入將被視為一個字符串。此外,完整的字符串將逐個字符地迭代,並且對於每個字符(即八進製數字),將根據上述八進製到二進製轉換表評估其等效二進製值。每次迭代的結果將添加到結果字符串中,所有值的組合將給出所需的二進製數。下麵是這種方法的實現。

Java


// Java program to convert
// Octal number to Binary
class OctalToBinary {
    // function to convert octal number
    // to its binary equivalent value
    static String converter(String octalValue)
    {
        // integer variable to iterate
        // the input octal string
        int i = 0;
        // string to store the result
        String binaryValue = "";
        // iterating the complete length
        // of octal string and assigning
        // the equivalent binary value
        // for each octal digit
        while (i < octalValue.length()) {
            // storing character according
            // to the number of iteration
            char c = octalValue.charAt((int)i);
            // switch case to check all
            // possible 8 conditions
            switch (c) {
            case '0':
                binaryValue += "000";
                break;
            case '1':
                binaryValue += "001";
                break;
            case '2':
                binaryValue += "010";
                break;
            case '3':
                binaryValue += "011";
                break;
            case '4':
                binaryValue += "100";
                break;
            case '5':
                binaryValue += "101";
                break;
            case '6':
                binaryValue += "110";
                break;
            case '7':
                binaryValue += "111";
                break;
            default:
                System.out.println(
                    "\nInvalid Octal Digit "
                    + octalValue.charAt((int)i));
                break;
            }
            i++;
        }
        // returning the final result
        return binaryValue;
    }
    // Driver code
    public static void main(String args[])
    {
        System.out.println("Octal to Binary Conversion\n");
        // octal number which is to be converted
        String octalNumber = "315";
        System.out.println("Octal number:" + octalNumber);
        // calling the converter method and
        // storing the result in a string variable
        String result = converter(octalNumber);
        System.out.println("Binary equivalent value is:"
                           + result);
    }
}
輸出
Octal to Binary Conversion

Octal number:315
Binary equivalent value is:011001101

方法2:數學方法

該方法涉及完整的數學計算以獲得所需的結果。八進製數的輸入將被視為整數。此外,它首先被轉換為其十進製等效值,然後使用數學運算從十進製數轉換為其二進製等效值。下麵是這種方法的實現。

Java


// Java program to convert
// Octal number to Binary
public class OctalToBinary {
    // function to convert octal number
    // to its binary equivalent value
    public static int converter(int octalValue)
    {
        // declaring all variable
        // to store the intermediate results
        int i = 0;
        int decimalValue = 0;
        int binaryValue = 0;
        // converting octal number
        // into its decimal equivalent
        while (octalValue != 0) {
            decimalValue
                += (octalValue % 10) * Math.pow(8, i);
            ++i;
            octalValue /= 10;
        }
        i = 1;
        // converting generated decimal number
        // to its binary equivalent
        while (decimalValue != 0) {
            binaryValue += (decimalValue % 2) * i;
            decimalValue /= 2;
            i *= 10;
        }
        // returning the final result
        return binaryValue;
    }
    // Driver code
    public static void main(String[] args)
    {
        System.out.println("Octal to Binary Conversion\n");
        // octal number which is to be converted
        int octalNumber = 315;
        System.out.println("Octal number:" + octalNumber);
        // calling the converter method and
        // storing the result in a string variable
        int result = converter(octalNumber);
        // printing the binary equivalent value
        System.out.println("Binary equivalent value is:"
                           + result);
    }
}
輸出
Octal to Binary Conversion

Octal number:315
Binary equivalent value is:11001101

方法3:使用內置的java方法

Integer.parseInt() 是 Java 中的內置函數,用於將字符串解析為由基數值(方法的第二個參數)指定的數字係統。在這種方法中,八進製數的輸入將被視為一個字符串。八進製字符串將被解析為八進製數字係統的整數值。此外,八進製數將使用另一個內置方法 Integer.toBinaryString() 轉換為其二進製等效值。結果值也將是字符串數據類型。下麵是實現。

Java


// Java program to convert
// Octal number to Binary
class OctalToBinary {
    // function to convert octal number
    // to its binary equivalent value
    public static String converter(String octalValue)
    {
        // parsing the string value
        // by following octal number system
        int octal = Integer.parseInt(octalValue, 8);
        // converting octal number to binary
        // and storing as a string
        String binaryValue = Integer.toBinaryString(octal);
        // returning the resultant string
        return binaryValue;
    }
    // Driver code
    public static void main(String args[])
    {
        System.out.println("Octal to Binary Conversion\n");
        // octal number which is to be converted
        String octalNumber = "315";
        System.out.println("Octal number:" + octalNumber);
        // calling the converter method and
        // storing the result in a string variable
        String result = converter(octalNumber);
        // printing the binary equivalent value
        System.out.println("Binary equivalent value is:"
                           + result);
    }
}
輸出
Octal to Binary Conversion

Octal number:315
Binary equivalent value is:11001101

相關用法


注:本文由純淨天空篩選整理自RISHU_MISHRA大神的英文原創作品 Java Program to Convert Octal to Binary。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。