Java basic series-5. Java function instances-hexadecimal conversion

Source: Internet
Author: User
Plain method: decimal --> binary

/* Decimal --> binary mod 2 returns the remainder Reverse Order */public static void Tobin (INT num) {stringbuffer sb = new stringbuffer (); While (Num> 0) {sb. append (Num % 2); num = num/2;} system. out. println (sb. reverse (); // reverse string}

Plain method: decimal --> hexadecimal

/* Decimal --> the last four digits of the binary value are converted into X/public static void tohex (INT num) {stringbuffer sb = new stringbuffer (); for (INT x = 0; x <8; X ++) // Int Is 4 8-bit, 0000-0000 0000-0000 0000-0000 0000-0000 {int temp = num & 15; // and 1111 with the operation, get the last four binary if (TEM> 9) Sb. append (char) (temp-10 + 'A'); // convert to the letter elsesb if it exceeds 9. append (temp); num = num >>> 4; // shifts 4 places to the right} system. out. println (sb. reverse ());}

Look-up method: decimal --> binary

/* Look-up table decimal --> binary */public static void Tobin (INT num) {// define the binary table char [] CHS = {'0', '1 '}; // define a temporary storage container. Char [] arr = new char [32]; int Pos = arr. length; while (num! = O) {int temp = num & 1; arr [-- POS] = CHS [temp]; num = num >>> 1 ;}for (INT x = Pos; x <arr. length; x +) {system. out. print (ARR [x]) ;}}

Look-up table method: decimal --> hexadecimal

/* Look-up table in decimal format --> hexadecimal 0 1 2 3 4 5 6 7 8 9 a B C D E F = element 0 1 2 3 4 5 6 in hexadecimal format 7 8 9 10 11 12 13 14 15 */public static void tohex (INT num) {char [] CHS = {'0', '1', '2', '3', '4', '5', '6', '7 ', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; // define a temporary container. Char [] arr = new char [8]; int Pos = arr. length; while (num! = 0) {int temp = num & 15; arr [-- POS] = CHS [temp]; // store num = num >>> 4;} from the last position of the array ;} for (INT x = Pos; A <arr. length; X ++) {system. out. print (ARR [x] + ',');}}

Optimize the lookup method: extract the public part

// Public static void trans (INT num, int base, int offset) {If (num = 0) {system. out. println (0); return;} Char [] CHS = {'0', '1', '2', '3', '4', '5 ', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E ', 'F'}; char [] arr = new char [32]; int Pos = arr. length; while (num! = 0) {int temp = num & base; arr [-- POS] = CHS [temp]; num = num >>> offset ;}for (INT x = Pos; x <arr. length; X ++) {system. out. print (ARR [x]) ;}/ * decimal --> binary */public static void Tobin (INT num) {TRANS (Num, 1, 1 );} /* decimal --> */public static void tooctal (INT num) {TRANS (Num, 7,3 );} /* decimal --> hexadecimal */public static void tohex (INT num) {TRANS (Num, 15,4 );}
Related Article

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.