/* ASCTAB.CPP */ #include #include char* binary(int); main() { int c, C; char left[9], right[9]; printf("\tASCII TABLE: Character, Decimal, Binary, Octal, Hex\n\n"); printf("Char Dec Binary Oct Hex\tChar Dec Binary Oct Hex\n"); for(c = 32; c <= 79; c++) { C = c + 48; strcpy(left, binary(c)); strcpy(right, binary(C)); printf(" %c %3d %s %3o %3X\t %c %3d %s %3o %3X\n", c, c, left, c, c, C, C, right, C, C); } 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; }