Hello, got a question regarding printing out the 128 first characters from the ascii table. I haven't gotten so far yet, because I already stumbled to a problem. The following code prints the correct value starting from 32-127. From 0 to 31 however it prints out some scrap values. I assume it is correct as well since I quick checkup on the asciitable.com those values represents something else beside the ABC...Z.
So my question is how I can print out those values as in the example output? And also I want to know how one should write a program which print out like the example output. Since the solutions I can think off would give me output e.g. 1 to 10 chars in a rows instead for in columns.
int main()
{
int i =0;
for(int rows = 0; rows < 16; rows++)
{
i = rows;
while(i <= 127)
{
if(i <= 15)
cout << hex << setw(2) << setfill('0') << i << "= " << setw(2) << setfill(' ') << toStr(i) << " | ";
else
cout << hex << setw(2) << setfill('0') << i << "= " << setw(2) << setfill(' ') << char(i) << " | ";
i+=16;
}
cout << "\n";
}
return 0;
}
Example output
+--------+--------+--------+--------+--------+--------+--------+--------+
| 00 | 10 | 20 ' ' | 30 '0' | 40 '@' | 50 'P' | 60 '`' | 70 'p' |
| 01 | 11 | 21 '!' | 31 '1' | 41 'A' | 51 'Q' | 61 'a' | 71 'q' |
| 02 | 12 | 22 '"' | 32 '2' | 42 'B' | 52 'R' | 62 'b' | 72 'r' |
| 03 | 13 | 23 '#' | 33 '3' | 43 'C' | 53 'S' | 63 'c' | 73 's' |
| 04 | 14 | 24 '$' | 34 '4' | 44 'D' | 54 'T' | 64 'd' | 74 't' |
| 05 | 15 | 25 '%' | 35 '5' | 45 'E' | 55 'U' | 65 'e' | 75 'u' |
| 06 | 16 | 26 '&' | 36 '6' | 46 'F' | 56 'V' | 66 'f' | 76 'v' |
| 07'\a' | 17 | 27 ''' | 37 '7' | 47 'G' | 57 'W' | 67 'g' | 77 'w' |
| 08'\b' | 18 | 28 '(' | 38 '8' | 48 'H' | 58 'X' | 68 'h' | 78 'x' |
| 09'\t' | 19 | 29 ')' | 39 '9' | 49 'I' | 59 'Y' | 69 'i' | 79 'y' |
| 0a'\n' | 1a | 2a '*' | 3a ':' | 4a 'J' | 5a 'Z' | 6a 'j' | 7a 'z' |
| 0b'\v' | 1b | 2b '+' | 3b ';' | 4b 'K' | 5b '[' | 6b 'k' | 7b '{' |
| 0c'\f' | 1c | 2c ',' | 3c '<' | 4c 'L' | 5c'\\' | 6c 'l' | 7c '|' |
| 0d'\r' | 1d | 2d '-' | 3d '=' | 4d 'M' | 5d ']' | 6d 'm' | 7d '}' |
| 0e | 1e | 2e '.' | 3e '>' | 4e 'N' | 5e '^' | 6e 'n' | 7e '~' |
| 0f | 1f | 2f '/' | 3f '?' | 4f 'O' | 5f '_' | 6f 'o' | 7f |
+--------+--------+--------+--------+--------+--------+--------+--------+
EDIT I got a quite ok solution now I think, edited the code. Beside understanding how to print in columns I also used the function which Martin nicely provided.