10 to 2 and 16, 10 to 16
// When I see the Forum saying that I encountered an interview, I tried to write public class Test {public static void main (String [] args) {System. out. println (toBinary (9); System. out. println (toHex (559); System. out. println (Integer. toBinaryString (9); System. out. println (Integer. toHexString (559);/* 100122F100122f */} public static String toBinary (int n) {StringBuilder temp = new StringBuilder (); while (n/2> = 1 | n % 2 = 1) {// except for 2, (| n % 2 = 1) in order to add the last 0 or 1temp. append (N % 2); n = n/2;} return temp. reverse (). toString ();}/** hexadecimal to hexadecimal: divide the given decimal integer by the base number 16, and the remainder is the hexadecimal decimal digit. Divide the quotient in the previous step by the base number 16, and the remainder is the same as the hexadecimal number. Repeat the previous step until the final operator is 0. The remainder obtained by each division is the number of users in hexadecimal notation. The remainder of the last Division is the highest bit */public static String toHex (int n) {/* Train of Thought: except 16 get remainder */StringBuilder temp = new StringBuilder (); while (n/16> = 1) {int aa = n/16; int bb = n % 16; // 0123456789 10 11 12 13 14 15 // 0123456789 a B C D E FString str = ""; if (bb = 10) {str = "";} else if (bb = 11) {str = "B";} else if (bb = 12) {str = "C";} else if (bb = 13) {str = "D";} else if (bb = 14) {str = "E";} else if (bb = 15) {str = "F ";} else {str = bb + "";} temp. append (str); n = aa; if (n/16 <1) {// Add the last temp. append (n) ;}} return temp. reverse (). toString ();}}
Conversion between binary, decimal, and hexadecimal
Computing is possible with a computer.
Start-Program-accessories-calculator-View-scientific type
It's easy to handle this.
For example, convert binary to 10101 to hexadecimal.
Point to 10101 on the binary. In the hexadecimal format, 21 is displayed.
OK.
Conversion between binary, decimal, and hexadecimal
Computing is possible with a computer.
Start-Program-accessories-calculator-View-scientific type
It's easy to handle this.
For example, convert binary to 10101 to hexadecimal.
Point to 10101 on the binary. In the hexadecimal format, 21 is displayed.
OK.