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

您所在的位置:网站首页 字符类型转化为int类型 C++程序 – char到int的转换

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

2024-04-26 12:19| 来源: 网络整理| 查看: 265

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

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

使用强制类型转换. 使用static_cast. Using sscanf(). Using stoi(). Using atoi(). 使用stringstream.

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

1. 使用强制类型转换

方法1:

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

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

// C++ program to convert // char to int using typecasting #include using namespace std;    // Driver code int main()  {     char ch = 'A';      cout // C++ program to convert char // to int using atoi() #include using namespace std;    // Driver code int main()  {   const char *str = "1234";      int y = atoi(str);    cout


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3