Converts a decimal into binary, octal, hexadecimal

Source: Internet
Author: User

 Packagecom.db2;/*** Converts a decimal into binary, octal, hex * *@authorDenny **/ Public classDemo2 { Public Static voidMain (string[] args) {toBin (6); ToBin (-6); Tooct (60); Tooct (-60); Tohex (60); Tohex (-60); }    //Convert 2 binary     Public Static voidToBin (intnum) {Totran (num,1, 1);//1 is 2 binary max, binary only 0,1,1 bit right shift    }    //octal     Public Static voidTOOCT (intnum) {Totran (num,7, 3);//7 is 2 binary maximum, octal only 0-7 Max is 7, 3 bit 2 binary represents an octal right shift 3 bit    }    //hexadecimal     Public Static voidTohex (intnum) {Totran (num,15, 4);//15 is hexadecimal maximum, hexadecimal only 0-15 Max is 15, 4 bit 2 binary represents an octal right shift 4 bit    }    //Conversion Functions    Private Static voidTotran (intNumintBaseintOffset) {//num number to convert , number to ask, number to shift right        Char[] ch = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F '};//hex element contains 2, binary, octal//stores the converted number//only 32-bit maximum        Char[] arr =New Char[32]; //defining the subscript for the control array        intpos =arr.length; //Start Conversion         while(num! = 0) {            inttemp = num & base;//with a bitArr[--pos] = ch[temp];//Locate and storenum = num >>>offset; }        //Output         for(inti = pos; i < arr.length; i++) {System.out.print (arr[i]);    } System.out.println (); }}

Converts a decimal into binary, octal, hexadecimal

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.