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

╰+攻爆jí腚メ 2023-09-25 14:17 57阅读 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++程序:

  1. // C++ program to convert
  2. // char to int using typecasting
  3. #include <iostream>
  4. using namespace std;
  5. // Driver code
  6. int main()
  7. {
  8. char ch = 'A';
  9. cout << int(ch);
  10. return 0;
  11. }

输出

  1. 65

方法2:

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

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

  1. // C++ program to convert
  2. // char to int using typecasting
  3. #include <iostream>
  4. using namespace std;
  5. // Driver code
  6. int main()
  7. {
  8. char ch = 'a';
  9. int N = int(ch);
  10. cout << N;
  11. return 0;
  12. }

输出

  1. 97

2. 使用 static_cast

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

  1. // C++ program to convert char
  2. // to int using static_cast
  3. #include <iostream>
  4. using namespace std;
  5. // Driver code
  6. int main()
  7. {
  8. char ch = 'A';
  9. int N = static_cast<int>(ch);
  10. cout << N;
  11. return 0;
  12. }

输出

  1. 65

3. 使用sscanf

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

  1. // C++ program to convert char
  2. // to int using sscanf()
  3. #include <iostream>
  4. using namespace std;
  5. // Driver code
  6. int main()
  7. {
  8. const char *s = "1234";
  9. int x;
  10. sscanf(s, "%d", &x);
  11. cout << "\nThe integer value of x : " << x;
  12. return 0;
  13. }

输出

  1. The integer value of x : 1234

4. 使用 stoi

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

  1. // C++ program to convert char
  2. // to int using stoi()
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6. // Driver code
  7. int main()
  8. {
  9. char s1[] = "45";
  10. int x = stoi(s1);
  11. cout << "The integer value of x : " << x;
  12. return 0;
  13. }

输出

  1. The integer value of x : 45

5. 使用 atoi

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

  1. // C++ program to convert char
  2. // to int using atoi()
  3. #include <iostream>
  4. using namespace std;
  5. // Driver code
  6. int main()
  7. {
  8. const char *str = "1234";
  9. int y = atoi(str);
  10. cout << "\nThe integer value of y :" << y;
  11. return 0;
  12. }

输出

  1. The integer value of y :1234

6. 使用stringstream

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

  1. // C++ program to convert char
  2. // to int using string stream
  3. #include <iostream>
  4. #include <string>
  5. #include <sstream>
  6. using namespace std;
  7. // Driver code
  8. int main()
  9. {
  10. stringstream string;
  11. string << "5";
  12. int n;
  13. string >> n;
  14. cout << "Integer value is: " << n;
  15. return 0;
  16. }

输出

  1. Integer value is: 5

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

发表评论

表情:
评论列表 (有 0 条评论,57人围观)

还没有评论,来说两句吧...

相关阅读