Binary conversion JAVA

Source: Internet
Author: User

1. Decimal----binary (method one)

1 /* 2 decimal---binary (method one)3 */4 classDecToBin15 {6      Public Static voidMain (string[] args)7     {8ToBin (6);9     }Ten  One      Public Static voidToBin (intnum) A     { -StringBuffer SB =NewStringBuffer ();//Container -          while(num>0) the         { -Sb.append (num%2);//Append: Add -Num/= 2; -         } +         //System.out.println (SB); //Counter Output -System.out.println (Sb.reverse ());//Positive Output +     } A}

2. Decimal---binary (method two)

1 class DecToBin2 2 {3      Public Static void  4    {5         System.out.println (integer.tobinarystring (6)); 6     }7 }

3. Binary--Hex

1 /*2 binary --hex3 */4 classBintohex5 {6      Public Static voidMain (string[] args)7     {8Tohex (60);9     }Ten  One      Public Static voidTohex (intnum) A     { -StringBuffer SB =NewStringBuffer (); -          for(inti=0; I<8; i++)//32 bytes Maximum of 8 groups the         { -             inttemp = num & 15; -             if(temp>9) -             { +Sb.append ((Char) (temp-10+ ' A ')); -             } +             Else A             { at sb.append (temp); -             } -num = num>>>4; -         } -SB =sb.reverse (); -          while(Sb.charat (0) = = ' 0 ')//Note the character 0 ' 0 ' in         { -Sb.deletecharat (0); to         } + System.out.println (SB);  -     } the}

/*
Tabular method: Set up a char array, the 0-F input, using the angle mark call in the establishment of an int array, store the distribution results, and finally through the method of the traversal to remove 0.
Program slightly.
*/

4. Final optimization (decimal turn binary; decimal to octal; decimal to hexadecimal)

1 classTrans2 {3      Public Static voidMain (string[] args)4     {5         /*Decimal Turn binary*/6ToBin (60);7         /*Decimal Turn octal*/8ToBa (60);9         /*Decimal Turn hex*/    TenTohex (60); One     } A  -      Public Static voidToBin (intnum) -     { theTrans (num,1,1); -     } -      -      Public Static voidToBa (intnum) +     { -Trans (num,7,3); +     } A      at      Public Static voidTohex (intnum) -     { -Trans (num,15,4); -     } -      -      Public Static voidTransintNumintBaseintoffset) in     { -         Char[] chs = {' 0 ', ' 1 ', ' 2 ', ' 3 ', to' 4 ', ' 5 ', ' 6 ', ' 7 ', +' 8 ', ' 9 ', ' A ', ' B ', -' C ', ' D ', ' E ', ' F '}; the         Char[] arr =New Char[32]; *         intpos =arr.length; $          while(num!=0)Panax Notoginseng         { -             inttemp = num &Base; theArr[--pos] =Chs[temp]; +num = num >>>offset; A         } the          for(intI=pos; I<arr.length; i++) +         { - System.out.print (Arr[i]); $         } $ System.out.println (); -     } -}

Binary conversion JAVA

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.