The principle analysis and source code of Java on the conversion of decimal to various bases

The principle analysis and source code of Java on the conversion of decimal to various bases


Insert picture description here

This article introduces the conversion between hexadecimals in detail and attaches Java code (similar to other languages) so that readers can better understand the conversion between hexadecimals, and can quickly and accurately perform the conversion through the source code provided by the author. I hope it can help everyone.
Here are some binary, octal, and hexadecimal websites for readers to learn!

Binary
octal
hexadecimal

Decimal to binary

First of all, when converting decimal to binary, the conversion between the integer part and the hour part is different.
Integer part: First, divide the decimal number (29 as shown in the figure), divide by 2 and take the remainder, write the remainder to the right, and continue the previous operations on the obtained quotient until the quotient is 0. The resulting binary number is the remainder read from bottom to top.
decimal part: As shown in the second example, we multiply the decimal fraction to be converted by two and round it up, that is, the ones digit of the number obtained by multiplying by 2 is used as the number on the right, and each digit is restored to 0, and the previous operation is continued until The fractional part is 0. Reading from top to bottom is the binary number we want to convert.

Insert picture description here

Decimal to octal

The method is similar to the conversion of decimal to binary, except that the divisor is changed to 8.

Decimal to Hexadecimal

The method is similar to the conversion of decimal to binary, except that the divisor is changed to 16.
Another point to note is: hexadecimal representation, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, FF, respectively 1-16.

Various bases to decimal conversion

前面介绍了各种十进制转其他进制的方法,为了文章的完整性和补充,这里再介绍一下其他进制向十进制的转换方法。

Insert picture description here
The figure above is the method of converting from binary to decimal. For other decimals, just change the base 2 in the formula shown in the above figure to the corresponding base; for example, binary is 2^(n-1);
octal is 8^(n -1);
Hexadecimal is 16^(n-1);
Let’s take a few examples. For the binary number 110101: x = 1 + 0 2 + 1 2 2 + 0 2 2 2 + 1 2 2 2 2 + 1 2 2 2 2 2;
octal number 721: x = 1 + 2
8 + 7 8 8;
hexadecimal number A962: x = 2 + 6*16 + 9 * 16 *16 + 10 *16 * 16 *16; (A in hexadecimal system A means 10)
The author also added the code to the source code above, with comments. Readers in need can copy and learn to use.

package monster;
/*
 * 该程序用来实现进制之间的转换
 * 有二进制、八进制、十进制和十六进制
 */
import java.util.*;
public class Conversion {
    
    

	public static void main(String[] args) {
    
    

		Conversion a = new Conversion();					// 申明对象
		System.out.println("please input an decimal number to change:");
		Scanner in = new Scanner(System.in);
		int num = in.nextInt();
		a.binary(num);										// 调用对象的方法
		a.octal(num);
		a.hexadecimal(num);
		in.close();
	}
	
	public void binary(int num){
    
    
		// 十进制转二进制
		System.out.println("the reverse binary number is:");
		while(true){
    
    
			System.out.print((num%2) +" ");
			num = num/2;
			if(num == 0) {
    
    
				break;
			}
		}
		System.out.println();
	}
	
	public void octal(int num){
    
    
		// 十进制转八进制
		System.out.println("the decimal number is:");
		while(true){
    
    
			System.out.print((num%8) +" ");
			num = num/8;
			if(num == 0) {
    
    
				break;
			}
		}
		System.out.println();
	}
	
	public void hexadecimal(int num){
    
    
		// 十进制转十六进制
		System.out.println("the hexadecimal number is:");
		while(true){
    
    
			System.out.print((num%16) +" ");
			num = num/16;
			if(num == 0) {
    
    
				break;
			}
		}
		System.out.println();
	}
}

Here is the code result:
Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/wlfyok/article/details/109030153