优秀的编程知识分享平台

网站首页 > 技术文章 正文

char和int强转(char和int转换)

nanyue 2024-07-23 13:46:24 技术文章 12 ℃

?强转的差是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;
}

大小写转换的差是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表

最后,如果你想学C/C++可以私信小编“01”获取素材资料以及开发工具和听课权限哦!

Tags:

最近发表
标签列表