Java程序演示八进制整数的使用
八进制整数是一种以8为基数,数字从0到7的数字系统。Int数据类型用于存储八进制数。
这里将讨论八进制数系统的用法:
将十进制转换为八进制
将八进制转换为十进制。
从十进制数系统到八进制数系统的转换
转换基础
要将任何十进制数转换为其等效的八进制数
持续将十进制数除以八进制数系统的基数8,直到商为0。
记住在每一步记录余数。
最后,以相反的顺序写下余数,得到的数就是对应的八进制数。
让我们看一些例子来更好地理解这个概念。
示例1 - 考虑十进制数2894,找出其八进制等效值。
Decimal Division Quotient Remainder 2894 2894 / 8 461 6 461 461 / 8 57 5 57 57 / 8 7 1 7 7 / 8 0 7
因此,7156是十进制数2894的等效八进制数。
示例2 - 考虑另一个十进制数101,找出其八进制等效值。
Decimal Division Quotient Remainder 101 101 / 8 12 5 12 12 / 8 1 4 1 1 / 8 0 1
因此,145是十进制数101的等效八进制数。
示例
以下程序使用自定义逻辑将十进制数转换为其对应的八进制数。在名为DecimalToOctal1的类中创建了一个名为DectoOctal且带有一个参数的方法,以演示八进制数系统的运作方式。这里,十进制数15作为实际参数传递给创建的方法,并使用自定义逻辑将其转换为对应的八进制数。
//Java Program to illustrate the decimal to octal conversion
//using custom logic
public 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: 17 Octal equivalent of decimal number 24 is: 30 Octal equivalent of decimal number 7 is: 7 Octal equivalent of decimal number 5 is: 5
示例
以下程序使用库函数将十进制数转换为其对应的八进制数。创建了一个名为DecimalToOctal2的类,其中四个不同的十进制数使用库函数Integer.toOctalString()转换为其对应的八进制值。
//Java Program to demonstrate the conversion of decimal number into its equivalent octal number using a library function
public 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:17 Octal equivalent of decimal number 24 is:30 Octal equivalent of decimal number 7 is:7 Octal 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的等效十进制数。
示例
以下程序使用自定义逻辑将十进制数转换为其对应的八进制数。在名为OctalToDecimal1的类中创建了一个名为OctToDec且带有一个参数的方法,以演示八进制数系统的运作方式。这里,四个不同的八进制数作为实际参数传递给创建的方法,并使用自定义逻辑将其转换为对应的十进制数。
//Java Program to illustrate the conversion of Octal to Decimal
//using custom logic
public 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: 443 Equivalent Decimal of octal number 71 is: 19 Equivalent Decimal of octal number 100 is: 64 Equivalent Decimal of octal number 88 is: 8
示例
以下程序使用库函数将八进制数转换为其对应的十进制数。创建了一个名为OctalToDecimal2的类,其中四个不同的八进制数作为输入传递给库方法Integer.parseInt(),并将其转换为对应的十进制数。
//Java Program to demonstrate the conversion of an octal number into its equivalent decimal number using a library function
public 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 443 Equivalent Decimal of octal number 673 is 19 Equivalent Decimal of octal number 673 is 64 Equivalent Decimal of octal number 673 is 8
结论
本文阐述了八进制数的使用。本文借助一些示例和Java程序讨论了将八进制数系统转换为十进制数系统以及反向转换的方法。
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP