/* * This program first judge whether a decimal number is entered, * then convert a decimal number * to two's complement representation, then covert two's complement representation * to four digits hexadecimal number. Last print out two ASCII characters */ public class Convert { public static void main (String[] args) { System.out.println(); System.out.println("Please enter a number."); System.out.println(); String inPutString,newString,junk; int lengthOfString,i,j; int m=0,integer=0,integerNumber=0; int lengthOfNewString; inPutString=SavitchIn.readLine(); // input a string from keyboard System.out.println(); System.out.println ("The string you entered is "+inPutString); System.out.println(); lengthOfString=inPutString.length(); // find the length of the string entered int[] factor=new int[lengthOfString]; int[] number=new int[lengthOfString]; for (i=0;i<=lengthOfString-1;i++) { number[i]=0; } for (i=0; i<=lengthOfString-1; i++) { factor[i]=0; } /* * To find whether the first character of inPutString is '-' * If inPutString conatin a '-', then define a newString * which is the same as inPutString except that newString * do not contain '-'. This is for convert a negative decimal nuber to * binary. After conversion is completed, this progarm will add 1 to * the most siganificant bit. So this will confirm that the number is negative */ if (inPutString.charAt(0)!='-') newString=inPutString; else { newString=inPutString.substring(1,lengthOfString); } /* * Judge whether the whether the inPutString is a decimal number */ lengthOfNewString=newString.length(); // positive number or negavive number without '-' for (j=0; j<=lengthOfNewString-1; j++) { if (newString.charAt(j)=='0') factor[j]=1; else if (newString.charAt(j)=='1') factor[j]=1; else if (newString.charAt(j)=='2') factor[j]=1; else if (newString.charAt(j)=='3') factor[j]=1; else if (newString.charAt(j)=='4') factor[j]=1; else if (newString.charAt(j)=='5') factor[j]=1; else if (newString.charAt(j)=='6') factor[j]=1; else if (newString.charAt(j)=='7') factor[j]=1; else if (newString.charAt(j)=='8') factor[j]=1; else if (newString.charAt(j)=='9') factor[j]=1; m=m+factor[j]; } if (m!=lengthOfNewString) { System.out.println("Sorry,the String you entered is not an integer."); System.out.println("Please try again."); System.out.println("Press enter key to continue."); junk=SavitchIn.readLine(); System.exit(0); } // Convert the string to decimal number for (j=0;j<=lengthOfNewString-1;j++) { if (newString.charAt(j)=='1') number[j]=1; else if (newString.charAt(j)=='1') number[j]=1; else if (newString.charAt(j)=='2') number[j]=2; else if (newString.charAt(j)=='3') number[j]=3; else if (newString.charAt(j)=='4') number[j]=4; else if (newString.charAt(j)=='5') number[j]=5; else if (newString.charAt(j)=='6') number[j]=6; else if (newString.charAt(j)=='7') number[j]=7; else if (newString.charAt(j)=='8') number[j]=8; else if (newString.charAt(j)=='9') number[j]=9; else if (newString.charAt(j)=='0') number[j]=0; } for (j=0;j<=lengthOfNewString-1;j++) { integerNumber=integerNumber+number[j]*(int)Math.pow(10,(lengthOfNewString-j-1)); } if (lengthOfString!=lengthOfNewString) integerNumber=-integerNumber; // restore '-' // to negative number if (integerNumber<-32768) // set boundary { System.out.println("The integer you entered is too small."); System.out.println("It should be between -32768 to 32767."); System.out.println("Press enter key to continue"); junk=SavitchIn.readLine(); System.exit(0); } if (integerNumber>32767) // set boundary { System.out.println("The integer you entered is too large."); System.out.println("It should be between -32768 to 32767."); System.out.println("Press enter key to continue"); junk=SavitchIn.readLine(); System.exit(0); } /* * begin to convert decimal number to binary */ int integerFactor; integerFactor=integerNumber; if (integerNumber<0) integerNumber=-integerNumber; if (integerNumber==32768) integerNumber=0; int[] bin=new int[16]; //define an array to contain 16 binary digits for (i=0;i<=15;i++) { bin[i]=0; } for (i=15;i>=1;i--) // convert to binary { bin[i]=integerNumber%2; integerNumber=integerNumber/2; } if (lengthOfString!=lengthOfNewString) //convert to one's complement represention { for (i=0;i<=15;i++) { if (bin[i]==1) bin[i]=0; else bin[i]=1; } } /* * Convert to two's complement representation */ int[] data=new int[16]; // array data reserve original digitss for (i=0;i<=15;i++) // to use to judge whether there is an { // change from 1 to 0 in the next loop data[i]=bin[i]; } if (lengthOfString!=lengthOfNewString) { if (bin[15]==0) bin[15]=1; else { bin[15]=0; for(i=14;i>=0;i--) { if (bin[i]==1&&data[i+1]==1) bin[i]=0; else if (bin[i]==1&&data[i+1]==0) { bin[i]=1; break; } else if (bin[i]==0) { bin[i]=1; break; } } } } if (integerFactor==-32768) // -32768 is special number, so consider it separately { bin[0]=1; for (i=1;i<=15;i++) { bin[i]=0; } } System.out.println("The two's complement representation of "+integerFactor+" is:"); System.out.println(); for (i=0;i<=15;i++) { System.out.print(bin[i]); } System.out.println(); /* * convert to hexadecimal number * */ char[] hex=new char[5]; for(i=1;i<=4;i++) { hex[i]='0'; } for(i=15;i>=3;i=i-4) { if (bin[i-3]==0&bin[i-2]==0&bin[i-1]==0&bin[i]==0) hex[(i+1)/4]='0'; if (bin[i-3]==0&bin[i-2]==0&bin[i-1]==0&bin[i]==1) hex[(i+1)/4]='1'; if (bin[i-3]==0&bin[i-2]==0&bin[i-1]==1&bin[i]==0) hex[(i+1)/4]='2'; if (bin[i-3]==0&bin[i-2]==0&bin[i-1]==1&bin[i]==1) hex[(i+1)/4]='3'; if (bin[i-3]==0&bin[i-2]==1&bin[i-1]==0&bin[i]==0) hex[(i+1)/4]='4'; if (bin[i-3]==0&bin[i-2]==1&bin[i-1]==0&bin[i]==1) hex[(i+1)/4]='5'; if (bin[i-3]==0&bin[i-2]==1&bin[i-1]==1&bin[i]==0) hex[(i+1)/4]='6'; if (bin[i-3]==0&bin[i-2]==1&bin[i-1]==1&bin[i]==1) hex[(i+1)/4]='7'; if (bin[i-3]==1&bin[i-2]==0&bin[i-1]==0&bin[i]==0) hex[(i+1)/4]='8'; if (bin[i-3]==1&bin[i-2]==0&bin[i-1]==0&bin[i]==1) hex[(i+1)/4]='9'; if (bin[i-3]==1&bin[i-2]==0&bin[i-1]==1&bin[i]==0) hex[(i+1)/4]='A'; if (bin[i-3]==1&bin[i-2]==0&bin[i-1]==1&bin[i]==1) hex[(i+1)/4]='B'; if (bin[i-3]==1&bin[i-2]==1&bin[i-1]==0&bin[i]==0) hex[(i+1)/4]='C'; if (bin[i-3]==1&bin[i-2]==1&bin[i-1]==0&bin[i]==1) hex[(i+1)/4]='D'; if (bin[i-3]==1&bin[i-2]==1&bin[i-1]==1&bin[i]==0) hex[(i+1)/4]='E'; if (bin[i-3]==1&bin[i-2]==1&bin[i-1]==1&bin[i]==1) hex[(i+1)/4]='F'; } System.out.println(); System.out.println("The hexademical of "+integerFactor+" is:"); System.out.println(); System.out.print(hex[1]); System.out.print(hex[2]); System.out.print(hex[3]); System.out.print(hex[4]); System.out.println(); /* * to convert two's representation to ASCII */ char ascii1,ascii2; int number2=bin[9]*(int)Math.pow(2,6)+bin[10]*(int)Math.pow(2,5)+bin[11]*(int)Math.pow(2,4)+bin[12]*(int)Math.pow(2,3)+bin[13]*(int)Math.pow(2,2)+bin[14]*2+bin[15]; int number1=bin[1]*(int)Math.pow(2,6)+bin[2]*(int)Math.pow(2,5)+bin[3]*(int)Math.pow(2,4)+bin[4]*(int)Math.pow(2,3)+bin[5]*(int)Math.pow(2,2)+bin[6]*2+bin[7]; if (number1<32||number1>126) { System.out.println("Sorry,the first ASCII character is unprintable."); System.out.println(" "); } else { ascii1=(char)number1; System.out.println("The first ASCII characters is:"); System.out.println(ascii1); } if (number2<32||number2>126) { System.out.println("Sorry,the second ASCII character is unprintable."); System.out.println(" "); } else { ascii2=(char)number2; System.out.println("The second ASCII characters is:"); System.out.println(ascii2); } } }