Common Methods for converting decimal to binary, octal, and hexadecimal

Source: Internet
Author: User
Tags decimal to binary
/*** Convert decimal to binary, octal, and hexadecimal * Note: * 1) an integer in Java is represented by four octal digits, that is, 32 binary digits * 2). A binary digit is represented by 0 and 1, and the maximum value is 1*3). An octal digit represents three binary digits, and the maximum value is 7*4) A hexadecimal digit represents four binary digits. The maximum value is 15 **/public class decimalconvert {public static void main (string [] ARGs) {int num = 60; system. out. println (Num + "converted to binary =" + tobinarystring (Num); system. out. println (Num + "converted to octal =" + tooctalstring (Num); system. out. println (Num + "converted to hexadecimal =" + tohexstring (Num ));} /*** Convert decimal to binary * @ Param num the integer to be converted to binary * @ return to the binary string */public static string tobinarystring (INT num) {return trans (Num, 1, 1 );} /*** convert decimal to octal * @ Param num to an octal integer * @ return to the octal string */public static string tooctalstring (INT num) {return trans (Num, 7, 3 );} /*** convert decimal to hexadecimal * @ Param num to a hexadecimal integer * @ return to a hexadecimal string */public static string tohexstring (INT num) {return trans (Num, 15, 4 );}/ * ** In the hexadecimal common calculation method, extract the same code for hexadecimal conversion * @ Param num the integer to be converted * @ Param base the hexadecimal base to be converted, if it is binary: base = 1; if it is octal: base = 7; if it is hexadecimal: base = 15 * @ Param offset, it is determined by the conversion hexadecimal type. Binary Offset: 1-bit, octal offset: 3-bit, hexadecimal offset: 4-bit * @ return: converted to the binary string */Private Static string trans (INT num, int base, int offset) {If (num = 0) Return ""; stringbuffer result = new stringbuffer (); // char [] CHS = new char [] {'0', '1', '2', '3 ', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D ', 'E', 'F'}; char [] arr = new char [32]; // in Java, int is represented by four binary octal bits, that is, 32 binary bits, therefore, the stored value cannot exceed 32-bit int Pos = arr. length; // pointer for storing the hexadecimal value // start the calculation until the valid bit is 0. Calculate the while (num! = 0) {// and (&) convert the maximum value of the hexadecimal valid bits. If a hexadecimal bits represent four binary bits in hexadecimal notation, the maximum value of the four binary bits is 15, so the base here is 15int temp = num & base; // It is stored from the maximum subscript bit for convenient retrieval and splicing. The temp value ranges from 0 to 15. Therefore, you can obtain the corresponding base value from the temp table. Arr [-- POS] = CHS [temp]; // unsigned shifts right. If it is in hexadecimal notation, it shifts four places to the right, because a hexadecimal digit represents four binary digits, so here the offset is 4num = num >>> offset ;}// Concatenates the calculated value into the corresponding hexadecimal representation, because it is stored in reverse mode, therefore, it is necessary to obtain the position of the last saved pointer to ensure that the displayed hexadecimal number is correct for (INT I = Pos; I <arr. length; I ++) {result. append (ARR [I]);} return result. tostring ();}}

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.