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

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

将二进制转换为八进制的Java程序

二进制数系统由两个符号组成:0(零)和 1(一个),它们分别代表数字电子中的低或关闭和高或开启状态。它主要是一个基数为 2 的数字系统,也广泛用于计算机科学。所有数据都存储在计算机中的二进制符号中,也称为位。二进制系统的名称源自它仅由两个符号组成的事实。二进制数也可以被认为是一个只有 0 和 1 的字符串。

八进制数系统包括从0 到 7的八位数字。它的名字来源于它由八位数字(因此是 Oct)组成,这意味着八位。它是一个 8 基数系统,可以通过将二进制数中的位分成三组并计算每组的相应值作为结果八进制数中的一位数来制定。

例子:

Input : 100100
Output: 44

Input : 1100001
Output : 141

在本文中,我们将探索两种将二进制数转换为八进制数的方法。这些方法如下:

  • 使用内置的toOctalString() Java中的方法
  • 将二进制数转换为十进制数,十进制数进一步转换为相应的八进制数。

方法一:

使用这种方法,我们首先将二进制数转换为整数,然后使用 Java的toOctalString()内置方法将其转换为八进制数的字符串。然后,该字符串再次转换为整数。

句法:

public static String toOctalString(int num)

参数:该方法接受整数类型的单个参数num ,需要将其转换为字符串。

返回值:该函数将整数参数的字符串表示形式返回为基数为 8 的无符号整数。

算法:

  1. 将二进制数转换为十进制数。
  2. 使用 toOctalString() 方法将此十进制数转换为八进制数字符串。
  3. 再次将此字符串转换为整数。

例子:

Java
// Java program to convert binary to octal
  
class GFG {
  
    // method to convert binary to decimal
    int binaryToDecimal(long binary)
    {
        // variable to store the decimal number
        int decimalNumber = 0, i = 0;
  
        // loop to extract the digits of the
        // binary number
        while (binary > 0) {
  
            // multiplying each digit of binary
            // with increasing powers of 2 towards
            // left
            decimalNumber
                += Math.pow(2, i++) * (binary % 10);
  
            // dividing the binary by 10
            // to remove the last digit
            binary /= 10;
        }
  
        // returning the converted decimal number
        return decimalNumber;
    }
  
    // function to convert decimal to octal
    int decimalToOctal(long binary)
    {
  
        // variable to store the decimal number
        // returned by the binaryToDecimal()
        int decimalNumber = binaryToDecimal(binary);
  
        // using the toOctalString() to convert
        // Integer to String of Octal Number
        String octalString
            = Integer.toOctalString(decimalNumber);
  
        // converting the String of octal number
        // to an Integer
        int octalNumber = Integer.parseInt(octalString);
  
        // returning the octal number
        return octalNumber;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
  
        // instantiating the class
        GFG ob = new GFG();
  
        // calling and printing the decimalToOCtal
        // method
        System.out.println("octal number:"+ob.decimalToOctal(100100));
    }
}


Java
// Java program to convert binary to octal
  
class GFG {
  
    // function to convert binary number
    // to decimal number
    int binaryToDecimal(long binary)
    {
  
        // variable to store the converted
        // decimal number
        int decimalNumber = 0, i = 0;
  
        // loop to convert binary to decimal
        while (binary > 0) {
  
            // extracting every digit of the
            // binary and multiplying with
            // increasing powers of 2
            decimalNumber
                += Math.pow(2, i++) * (binary % 10);
  
            // dividing the number by 10
            // to remove the last digit
            binary /= 10;
        }
  
        // returning the converted decimal
        // number
        return decimalNumber;
    }
  
    // function to convert decimal number
    // to octal
    int decimalToOctal(long binary)
    {
  
        // variable to store the octal number
        int octalNumber = 0, i = 0;
  
        // variable to store the output
        // returned by the binaryToDecimal()
        int decimalNumber = binaryToDecimal(binary);
  
        // loop to convert decimal to octal
        while (decimalNumber != 0) {
  
            // extracting the remainder on
            // multiplying by 8 and
            // dividing that with increasing
            // powers of 10
            octalNumber += (decimalNumber % 8)
                           * ((int)Math.pow(10, i++));
  
            // removing the last digit by
            // dividing by 8
            decimalNumber /= 8;
        }
  
        // returning the converted octal number
        return octalNumber;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
  
        // instantiating the class
        GFG ob = new GFG();
  
        // calling and printing the
        // decimalToOctal() function
        System.out.println(ob.decimalToOctal(1001001));
    }
}


输出
octal number:44

方法二:

使用这种方法,我们首先将二进制数转换为十进制数。然后我们通过连续提取余数并除以 8 将这个十进制数转换为八进制数。

算法:

  1. 将二进制数转换为十进制数。
  2. 现在,对于这个转换后的十进制数,运行一个 while 循环,直到这个数字变为 0。
  3. 在循环的每次迭代中,将数字除以 8 得到余数。
  4. 将此余数乘以 10 的递增幂。
  5. 最后将原数除以 8。

例子:

Java

// Java program to convert binary to octal
  
class GFG {
  
    // function to convert binary number
    // to decimal number
    int binaryToDecimal(long binary)
    {
  
        // variable to store the converted
        // decimal number
        int decimalNumber = 0, i = 0;
  
        // loop to convert binary to decimal
        while (binary > 0) {
  
            // extracting every digit of the
            // binary and multiplying with
            // increasing powers of 2
            decimalNumber
                += Math.pow(2, i++) * (binary % 10);
  
            // dividing the number by 10
            // to remove the last digit
            binary /= 10;
        }
  
        // returning the converted decimal
        // number
        return decimalNumber;
    }
  
    // function to convert decimal number
    // to octal
    int decimalToOctal(long binary)
    {
  
        // variable to store the octal number
        int octalNumber = 0, i = 0;
  
        // variable to store the output
        // returned by the binaryToDecimal()
        int decimalNumber = binaryToDecimal(binary);
  
        // loop to convert decimal to octal
        while (decimalNumber != 0) {
  
            // extracting the remainder on
            // multiplying by 8 and
            // dividing that with increasing
            // powers of 10
            octalNumber += (decimalNumber % 8)
                           * ((int)Math.pow(10, i++));
  
            // removing the last digit by
            // dividing by 8
            decimalNumber /= 8;
        }
  
        // returning the converted octal number
        return octalNumber;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
  
        // instantiating the class
        GFG ob = new GFG();
  
        // calling and printing the
        // decimalToOctal() function
        System.out.println(ob.decimalToOctal(1001001));
    }
}
输出
111