美文网首页
char与int的强转操作

char与int的强转操作

作者: isjinhao | 来源:发表于2018-07-22 23:43 被阅读0次

  强转的差是48

#include <bits/stdc++.h> 
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 <bits/stdc++.h> 
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的强转操作

      本文链接:https://www.haomeiwen.com/subject/blarmftx.html