/* ASCTAB2.CPP */ #include #include #include char* binary(int); //prototype for binary() [below] main() { int c, C; char left[9], right[9]; cout << "\tASCII TABLE: Character, Decimal, Binary, Octal, Hex\n\n"; cout << "Char Dec Binary Oct Hex\tChar Dec Binary Oct Hex\n"; for(c = 32; c <= 79; c++) { C = c + 48; //c for left set of columns //C for right set strcpy(left, binary(c)); //string of bits set up corresponding to c strcpy(right, binary(C)); //same for C cout << setw(3) //character version of c << (char) c //cast it << dec //back to decimal from previous time << setw(6) //through loop << c << setw(9) //provide for extra space to left << left //equivalent bits << oct //setup for octal version << setw(5) << c << hex //setup for hex version << setw(5) << c << '\t' //tab over to second column << setw(3) //for character version, as in left column << (char) C //print after casting << dec //next the decimal version << setw(6) << C << setw(9) << right //equivalent bits << oct //setup for octal << setw(5) << C << hex //setup for hex << setw(5) << C << endl; } return 0; } // BINARY.CPP CONVERTS INTEGER ARGUMENT N TO A STRING OF 0s AND 1s char* binary(int n) { static char bin[9]; // holds 0s or 1s for(int i=0; i<8; i++) bin[i] = '0'; // all 0s to begin with // FILL IN BINARY STRING FROM THE LEAST SIGNIFICANT BIT (=LSB) i = 7; while(n) // when n becomes 0, stop { if( n % 2 ) // if remainder = 1 (not 0 [false]) bin[i] = '1'; // 1 goes into string--stays 0 otherwise n >>= 1; // next n i--; // go left in the string } return bin; }