char与int的强转操作

  强转的差是48

#include  
using namespace std;
int main()
{
    int t1 = 9;
    char t2 = 9;
    //int强转为char:强转成该ASCII值下的char 
    cout << char(t1) << endl;
    //char强转为int:看起来相等 
    cout << int(t2) << endl;
    
    //想要相互之间看起来相等... 
    char r1 = char(t1 + 48);
    int r2 = int(t2);
    cout << r1 << " " << r2;
    return 0;
}
Console

  大小写转换的差是32

#include  
using namespace std;
int main()
{
    //大写字母的ASCII值小
    //小写字母的ASCII值大
    //相差的值为 :32 
    char t3 = 'A';
    char r3 = int(t3 + 32);
    cout << r3;  //Console:a 
    return 0;
}

  附上一张ASCII表

ASCII表

你可能感兴趣的:(char与int的强转操作)