这里将讨论八进制数系统的使用方式 −
将十进制转换为八进制
将八进制转换为十进制。
从十进制数字系统到八进制数字系统的转换转换的基础知识将任何十进制数转换为相应的八进制数:
继续将十进制数除以八进制数系统的基数 8,直到收到商 0。
记得在每一步都记录余数。
最后将余数倒着写出,得到的数就是对应的八进制数。
让我们看一些示例,以便更好地理解这个概念。
示例 1 - 考虑十进制数 2894 并找出其等价的八进制数。
decimal division quotient remainder2894 2894 / 8 461 6461 461 / 8 57 557 57 / 8 7 17 7 / 8 0 7
因此,7156 是十进制数 2894 的等价八进制数。
示例2 - 考虑另一个十进制数 101 并找出其八进制等价物。
decimal division quotient remainder101 101 / 8 12 512 12 / 8 1 41 1 / 8 0 1
因此,145 是十进制数 101 的等价八进制数。
example的中文翻译为:示例以下程序使用自定义逻辑将十进制数转换为其相应的八进制数。在名为 decimaltooctal1 的类中创建带有 1 个参数的名为 dectooctal 的方法,以演示八进制数字系统的工作原理。此处,十进制数 15 作为实际参数传递给创建的方法,并使用自定义逻辑将其转换为相应的八进制数。
//java program to illustrate the decimal to octal conversion//using custom logicpublic class decimaltooctal1 { //creating a method to convert decimal numbers into octal numbers public static string dectooctal(int dec) { int r; string octal=; //declaring and initializing an array octalchr char octalchr[]= {'0', '1', '2', '3', '4', '5', '6', '7'}; //logic for conversion while(dec>0) { r=dec%8; octal=octalchr[r]+octal; dec=dec/8; } return octal; } public static void main(string args[]) { //invoking the above method to get the octal number of the below decimal numbers system.out.println(octal equivalent of decimal number 15 is: +dectooctal(15)); system.out.println(octal equivalent of decimal number 24 is: +dectooctal(24)); system.out.println(octal equivalent of decimal number 7 is: +dectooctal(7)); system.out.println(octal equivalent of decimal number 5 is: +dectooctal(5)); }}
输出octal equivalent of decimal number 15 is: 17octal equivalent of decimal number 24 is: 30octal equivalent of decimal number 7 is: 7octal equivalent of decimal number 5 is: 5
example的中文翻译为:示例以下程序使用库函数将十进制数转换为对应的八进制数。在一个名为decimaltooctal2的类中,使用库函数integer.tooctalstring()将四个不同的十进制数转换为它们对应的八进制值。
//java program to demonstrate the conversion of decimal number into its equivalent octal number using a library functionpublic class decimaltooctal2 { public static void main(string args[]) { //by the usage of the integer.tooctalstring() library method //to convert a decimal value into a corresponding octal number system.out.println(octal equivalent of decimal number 15 is: +integer.tooctalstring(15)); system.out.println(octal equivalent of decimal number 24 is: +integer.tooctalstring(24)); system.out.println(octal equivalent of decimal number 7 is: +integer.tooctalstring(7)); system.out.println(octal equivalent of decimal number 5 is: +integer.tooctalstring(5)); }}
输出octal equivalent of decimal number 15 is:17octal equivalent of decimal number 24 is:30octal equivalent of decimal number 7 is:7octal equivalent of decimal number 5 is:5
从八进制数字系统到十进制数字系统的转换转换的基础知识要将任何八进制数转换为其等效的十进制数:
将十进制数中的每一位数字与八进制数的底数 8 的减幂(直到 0 次幂)相乘。
让我们看一些示例,以便更好地理解这个概念。
示例1 − 让我们考虑一个八进制数456 并找出它的十进制等价数。
456 = (4 * 8^2) + (5 * 8^1) + (6 * 8^0) = (4 * 64) + (5 * 8) + (6 * 1) = 256 + 40 + 6 = 302
因此,302 是八进制数 456 的等价十进制数。
示例2 - 让我们考虑另一个八进制数152并找出它的十进制等价数。
152 = (1 * 8^2) + (5 * 8^1) + (2 * 8^0) = (1 * 64) + (5 * 8) + (2 * 1) = 64 + 40 + 2 = 106
因此,106是八进制数152的等价十进制数。
example的中文翻译为:示例以下程序使用自定义逻辑将十进制数转换为其相应的八进制数。在名为 octaltodecimal1 的类中创建带有 1 个参数的名为 octtodec 的方法,以演示八进制数字系统的工作原理。这里,四个不同的八进制数作为实际参数传递给创建的方法,并使用自定义逻辑将其转换为相应的十进制数。
//java program to illustrate the conversion of octal to decimal//using custom logicpublic class octaltodecimal1 { //declaring a method to perform a conversion public static int octtodec(int oct) { //declaring variable to store a decimal number int dec = 0; //declaring variable to use in power int p = 0; //generating the logic while(true) { if(oct == 0) { break; } else { int t = oct % 10; dec += t * math.pow(8, p); oct = oct / 10; p++; } } return dec; } public static void main(string args[]) { //displaying the result of the conversion system.out.println(equivalent decimal of octal number 673 is: +octtodec(673)); system.out.println( equivalent decimal of octal number 71 is: +octtodec(23)); system.out.println(equivalent decimal of octal number 100 is: +octtodec(100)); system.out.println(equivalent decimal of octal number 88 is: +octtodec(10)); }}
输出equivalent decimal of octal number 673 is: 443equivalent decimal of octal number 71 is: 19equivalent decimal of octal number 100 is: 64equivalent decimal of octal number 88 is: 8
example的中文翻译为:示例以下程序使用库函数将八进制数转换为相应的十进制数。在一个名为octaltodecimal2的类中,将四个不同的八进制数作为输入传递给库方法integer.parseint(),并将其转换为相应的十进制数。
//java program to demonstrate the conversion of an octal number into its equivalent decimal number using a library functionpublic class octaltodecimal2 { public static void main(string args[]) { system.out.println(equivalent decimal of octal number 673 is + integer.parseint(673,8)); system.out.println(equivalent decimal of octal number 673 is + integer.parseint(23,8)); system.out.println(equivalent decimal of octal number 673 is + integer.parseint(100,8)); system.out.println(equivalent decimal of octal number 673 is + integer.parseint(10,8)); }}
输出equivalent decimal of octal number 673 is 443equivalent decimal of octal number 673 is 19equivalent decimal of octal number 673 is 64equivalent decimal of octal number 673 is 8
结论本文阐明了八进制数的用法。这里通过一些例子和java程序讨论了八进制和十进制之间的转换方法。
以上就是java程序示例,说明八进制整数的使用方法的详细内容。
