C++-容器-string:char到int的转换【Ascii码强转:int(char);‘0‘转为48】【直接转换:char - ‘0‘】

C++程序 – char到int的转换

在这里,我们将看到如何使用c++程序将char转换为int。c++中有6种将char型转换为int型的方法:

  1. 使用强制类型转换.
  2. 使用static_cast.
  3. Using sscanf().
  4. Using stoi().
  5. Using atoi().
  6. 使用stringstream.

让我们详细讨论每一种方法。

1. 使用强制类型转换

方法1:

  1. 声明并初始化要转换的字符。
  2. 使用int类型对字符进行类型转换,将字符转换为int类型。
  3. 使用cout打印整数。

下面是使用类型转换将char类型转换为int类型的c++程序:

// C++ program to convert
// char to int using typecasting
#include <iostream>
using namespace std;
  
// Driver code
int main() 
{
    char ch = 'A'; 
    cout << int(ch);
    return 0;
}

输出

65

方法2:

  1. 声明并初始化要转换的字符。
  2. 声明另一个变量为int N,并将字符ch赋值给N。
  3. 使用cout打印整数。

下面是使用类型转换将char类型转换为int类型的c++程序:

// C++ program to convert 
// char to int using typecasting 
#include <iostream>
using namespace std;
  
// Driver code
int main() 
{
    char ch = 'a'; 
    int N = int(ch);
    cout << N;
    return 0;
}

输出

97

2. 使用 static_cast

可以使用static_cast函数将字符转换为整数。下面是使用static_cast将char转换为int的c++程序:

// C++ program to convert char
// to int using static_cast
#include <iostream>
using namespace std;
  
// Driver code
int main() 
{
    char ch = 'A'; 
    int N = static_cast<int>(ch); 
    cout << N;
    return 0;
}

输出

65

3. 使用sscanf

从s中读取数据,并将其存储在由形参格式中的附加参数指定的位置。下面是使用sscanf()将char转换为int的c++程序:

// C++ program to convert char
// to int using sscanf()
#include <iostream>
using namespace std;
  
// Driver code
int main() 
{
  const char *s = "1234"; 
  int x;
  sscanf(s, "%d", &x); 
  cout << "\nThe integer value of x : " << x;
  return 0;
}

输出

The integer value of x : 1234

4. 使用 stoi

c++中的stoi()函数将字符串转换为整数值。下面是使用stoi()将char转换为int的c++程序:

// C++ program to convert char
// to int using stoi()
#include <iostream>
#include <string>
using namespace std;
  
// Driver code
int main() 
{
  char s1[] = "45";
  int x = stoi(s1);
  cout << "The integer value of x : " << x;
  return 0;
}

输出

The integer value of x : 45

5. 使用 atoi

如果执行成功,atoi()方法将返回转换后的整数值。如果给定的字符串不能转换为整数,它将返回0。下面是使用atoi()将char转换为int的c++程序:

// C++ program to convert char
// to int using atoi()
#include <iostream>
using namespace std;
  
// Driver code
int main() 
{
  const char *str = "1234";
  
  int y = atoi(str); 
  cout << "\nThe integer value of y :" << y;
  return 0;
}

输出

The integer value of y :1234

6. 使用stringstream

stringstream将一个字符串对象连接到一个流,允许你像读取一个流一样读取它(比如cin)。Stringstream需要包含sstream头文件。stringstream类在处理输入时很有用。
下面是使用string流将char转换为int的c++程序:

// C++ program to convert char
// to int using string stream
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
  
// Driver code
int main() 
{
    stringstream string; 
    string << "5"; 
    int n; 
    string >> n;
    cout << "Integer value is: " << n;
    return 0;
}

输出

Integer value is: 5

C++程序 – char到int的转换|极客教程

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值