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