c++字符类型转化int类型
时间: 2023-06-02 08:04:44 浏览: 56
可以使用ASCII码进行转化,如下代码:
```c++
char c = '2';
int num = c - '0';
```
也可以使用函数进行转化,如下代码:
```c++
char c = '2';
int num = atoi(&c);
```
相关问题
c++字符串强制类型转换int
可以使用 stoi 函数将字符串强制类型转换为 int 类型。例如:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "123";
int num = stoi(str);
cout << num << endl; // 输出 123
return 0;
}
```
C++string类型转换成int类型
可以使用C库函数atoi()将字符串转换成整型。
示例代码:
```
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[10] = "12345";
int num = atoi(str);
printf("%d\n", num);
return 0;
}
```
输出结果为:
```
12345
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)