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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。