10 to 2 and 16, 10 to 16

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.