char和int强转

  强转的差是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表

在C语言中,将char类型换为int类型有几种方法可以实现。一种方法是使用类型换运算符,如引用和所示。下面是一个示例程序: ```c #include <iostream> using namespace std; int main() { char ch = 'A'; int converted = int(ch); cout << converted; return 0; } ``` 在这个程序中,我们将字符'A'赋值给了变量ch,然后使用`int(ch)`进行类型换。最后,我们将换后的结果打印出来。 另一种方法是使用C++的`stoi()`函数,如引用所示。这个函数可以将字符串换为整数值。下面是一个使用`stoi()`函数将字符换为整数的示例程序: ```c #include <iostream> #include <string> using namespace std; int main() { char s1[] = "45"; int converted = stoi(s1); cout << "The integer value of x: " << converted; return 0; } ``` 在这个程序中,我们将字符串"45"赋值给了字符数组s1,然后使用`stoi()`函数将字符串换为整数。最后,我们将换后的结果打印出来。 可以根据具体的需求选择适合的方法来进行charint换。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [C++-容器-string:charint换【Ascii码强转int(char);‘0‘为48】【直接换:char - ‘0‘】](https://blog.csdn.net/u013250861/article/details/128414963)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值