不假定任何字符具有一般性的值,我们可能得到更具有可移植性的代码。
#include <iostream>
#include <ctype.h>
#include <iomanip>
using std::cout;
using std::hex;
int main()
{
for (char ch = 'a'; ch <= 'z'; ch++) {
cout<<"the char is "<<ch<<" and the value is "<<(int)ch<<'\n';
}
for (char num = '0'; num <= '9'; num++) {
cout<<"the char is "<<num<<" and the value is "<<(int)num<<'\n';
}
for ( unsigned char pch = 0; pch !=255; pch++) {
if (isprint(pch)) {
cout<< "the char can be printed is: "<<pch<<" and the hex is: 0x"<<hex<<(unsigned int)pch<<"\n";
}
}
}
运行结果:
[chaos@localhost cpp]$ g++ -o 4.4 ./4.4.cpp
[chaos@localhost cpp]$ ./4.4
the char is a and the value is 97
the char is b and the value is 98
the char is c and the value is 99
the char is d and the value is 100
the char is e and the value is 101
the char is f and the value is 102
the char is g and the value is 103
the char is h and the value is 104
the char is i and the value is 105
the char is j and the value is 106
the char is k and the value is 107
the char is l and the value is 108
the char is m and the value is 109
the char is n and the value is 110
the char is o and the value is 111
the char is p and the value is 112
the char is q and the value is 113
the char is r and the value is 114
the char is s and the value is 115
the char is t and the value is 116
the char is u and the value is 117
the char is v and the value is 118
the char is w and the value is 119
the char is x and the value is 120
the char is y and the value is 121
the char is z and the value is 122
the char is 0 and the value is 48
the char is 1 and the value is 49
the char is 2 and the value is 50
the char is 3 and the value is 51
the char is 4 and the value is 52
the char is 5 and the value is 53
the char is 6 and the value is 54
the char is 7 and the value is 55
the char is 8 and the value is 56
the char is 9 and the value is 57
the char can be printed is: and the hex is: 0x20
the char can be printed is: ! and the hex is: 0x21
the char can be printed is: " and the hex is: 0x22
the char can be printed is: # and the hex is: 0x23
the char can be printed is: $ and the hex is: 0x24
the char can be printed is: % and the hex is: 0x25
the char can be printed is: & and the hex is: 0x26
the char can be printed is: ' and the hex is: 0x27
the char can be printed is: ( and the hex is: 0x28
the char can be printed is: ) and the hex is: 0x29
the char can be printed is: * and the hex is: 0x2a
the char can be printed is: + and the hex is: 0x2b
the char can be printed is: , and the hex is: 0x2c
the char can be printed is: - and the hex is: 0x2d
the char can be printed is: . and the hex is: 0x2e
the char can be printed is: / and the hex is: 0x2f
the char can be printed is: 0 and the hex is: 0x30
the char can be printed is: 1 and the hex is: 0x31
the char can be printed is: 2 and the hex is: 0x32
the char can be printed is: 3 and the hex is: 0x33
the char can be printed is: 4 and the hex is: 0x34
the char can be printed is: 5 and the hex is: 0x35
the char can be printed is: 6 and the hex is: 0x36
the char can be printed is: 7 and the hex is: 0x37
the char can be printed is: 8 and the hex is: 0x38
the char can be printed is: 9 and the hex is: 0x39
the char can be printed is: : and the hex is: 0x3a
the char can be printed is: ; and the hex is: 0x3b
the char can be printed is: < and the hex is: 0x3c
the char can be printed is: = and the hex is: 0x3d
the char can be printed is: > and the hex is: 0x3e
the char can be printed is: ? and the hex is: 0x3f
the char can be printed is: @ and the hex is: 0x40
the char can be printed is: A and the hex is: 0x41
the char can be printed is: B and the hex is: 0x42
the char can be printed is: C and the hex is: 0x43
the char can be printed is: D and the hex is: 0x44
the char can be printed is: E and the hex is: 0x45
the char can be printed is: F and the hex is: 0x46
the char can be printed is: G and the hex is: 0x47
the char can be printed is: H and the hex is: 0x48
the char can be printed is: I and the hex is: 0x49
the char can be printed is: J and the hex is: 0x4a
the char can be printed is: K and the hex is: 0x4b
the char can be printed is: L and the hex is: 0x4c
the char can be printed is: M and the hex is: 0x4d
the char can be printed is: N and the hex is: 0x4e
the char can be printed is: O and the hex is: 0x4f
the char can be printed is: P and the hex is: 0x50
the char can be printed is: Q and the hex is: 0x51
the char can be printed is: R and the hex is: 0x52
the char can be printed is: S and the hex is: 0x53
the char can be printed is: T and the hex is: 0x54
the char can be printed is: U and the hex is: 0x55
the char can be printed is: V and the hex is: 0x56
the char can be printed is: W and the hex is: 0x57
the char can be printed is: X and the hex is: 0x58
the char can be printed is: Y and the hex is: 0x59
the char can be printed is: Z and the hex is: 0x5a
the char can be printed is: [ and the hex is: 0x5b
the char can be printed is: \ and the hex is: 0x5c
the char can be printed is: ] and the hex is: 0x5d
the char can be printed is: ^ and the hex is: 0x5e
the char can be printed is: _ and the hex is: 0x5f
the char can be printed is: ` and the hex is: 0x60
the char can be printed is: a and the hex is: 0x61
the char can be printed is: b and the hex is: 0x62
the char can be printed is: c and the hex is: 0x63
the char can be printed is: d and the hex is: 0x64
the char can be printed is: e and the hex is: 0x65
the char can be printed is: f and the hex is: 0x66
the char can be printed is: g and the hex is: 0x67
the char can be printed is: h and the hex is: 0x68
the char can be printed is: i and the hex is: 0x69
the char can be printed is: j and the hex is: 0x6a
the char can be printed is: k and the hex is: 0x6b
the char can be printed is: l and the hex is: 0x6c
the char can be printed is: m and the hex is: 0x6d
the char can be printed is: n and the hex is: 0x6e
the char can be printed is: o and the hex is: 0x6f
the char can be printed is: p and the hex is: 0x70
the char can be printed is: q and the hex is: 0x71
the char can be printed is: r and the hex is: 0x72
the char can be printed is: s and the hex is: 0x73
the char can be printed is: t and the hex is: 0x74
the char can be printed is: u and the hex is: 0x75
the char can be printed is: v and the hex is: 0x76
the char can be printed is: w and the hex is: 0x77
the char can be printed is: x and the hex is: 0x78
the char can be printed is: y and the hex is: 0x79
the char can be printed is: z and the hex is: 0x7a
the char can be printed is: { and the hex is: 0x7b
the char can be printed is: | and the hex is: 0x7c
the char can be printed is: } and the hex is: 0x7d
the char can be printed is: ~ and the hex is: 0x7e