如何实现整型与字符串之间的相互转换?

2026-04-29 16:213阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计519个文字,预计阅读时间需要3分钟。

如何实现整型与字符串之间的相互转换?

c// 使用C语言将字符串转换为整型#include #include

char *cstr; // 字符串指针,可以是char*或const char*类型int num;

num=atoi(str); // 将字符串转换为整型num=strtol(cstr, NULL, 10); // 使用strtol函数,10表示十进制

// C++11中可以使用以下方法void test1() { std::string str1=1; std::string str2=1.5; // 此处省略具体实现}

flyfish

字符串转整型

C的方法 cstr是char*或者const char*类型的字符串

int num = atoi(str);
int num = strtol(cstr, NULL, 10);

//10 表示进制

C++11的方法

void test1() { std::string str1 = "1"; std::string str2 = "1.5"; std::string str3 = "1 with words"; int myint1 = std::stoi(str1); int myint2 = std::stoi(str2); int myint3 = std::stoi(str3); std::cout << "std::stoi(\"" << str1 << "\") is " << myint1 << '\n'; std::cout << "std::stoi(\"" << str2 << "\") is " << myint2 << '\n'; std::cout << "std::stoi(\"" << str3 << "\") is " << myint3 << '\n'; }

结果输出

std::stoi(“1”) is 1 std::stoi(“1.5”) is 1 std::stoi(“1 with words”) is 1

//源码参考cplusplus.com

void test2() { std::string str_dec = "2001, A Space Odyssey"; std::string str_hex = "40c3"; std::string str_bin = "-10010110001"; std::string str_auto = "0x7f"; std::string::size_type sz; // alias of size_t int i_dec = std::stoi (str_dec,&sz); int i_hex = std::stoi (str_hex,nullptr,16); int i_bin = std::stoi (str_bin,nullptr,2); int i_auto = std::stoi (str_auto,nullptr,0); std::cout << str_dec << ": " << i_dec << " and [" << str_dec.substr(sz) << "]\n"; std::cout << str_hex << ": " << i_hex << '\n'; std::cout << str_bin << ": " << i_bin << '\n'; std::cout << str_auto << ": " << i_auto << '\n'; return 0; }

输出

2001, A Space Odyssey: 2001 and [, A Space Odyssey] 40c3: 16579 -10010110001: -1201 0x7f: 127

其他类型 类似

无符号整型

stoul

如何实现整型与字符串之间的相互转换?

浮点型

stof

数值转字符串

std::string s;
s = std::to_string(1) + ” is int, “;

其他数值类型 类似

s = std::to_string(3.14f) + ” is float.”;

以上这篇C++ 整型与字符串的互转方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持自由互联。

本文共计519个文字,预计阅读时间需要3分钟。

如何实现整型与字符串之间的相互转换?

c// 使用C语言将字符串转换为整型#include #include

char *cstr; // 字符串指针,可以是char*或const char*类型int num;

num=atoi(str); // 将字符串转换为整型num=strtol(cstr, NULL, 10); // 使用strtol函数,10表示十进制

// C++11中可以使用以下方法void test1() { std::string str1=1; std::string str2=1.5; // 此处省略具体实现}

flyfish

字符串转整型

C的方法 cstr是char*或者const char*类型的字符串

int num = atoi(str);
int num = strtol(cstr, NULL, 10);

//10 表示进制

C++11的方法

void test1() { std::string str1 = "1"; std::string str2 = "1.5"; std::string str3 = "1 with words"; int myint1 = std::stoi(str1); int myint2 = std::stoi(str2); int myint3 = std::stoi(str3); std::cout << "std::stoi(\"" << str1 << "\") is " << myint1 << '\n'; std::cout << "std::stoi(\"" << str2 << "\") is " << myint2 << '\n'; std::cout << "std::stoi(\"" << str3 << "\") is " << myint3 << '\n'; }

结果输出

std::stoi(“1”) is 1 std::stoi(“1.5”) is 1 std::stoi(“1 with words”) is 1

//源码参考cplusplus.com

void test2() { std::string str_dec = "2001, A Space Odyssey"; std::string str_hex = "40c3"; std::string str_bin = "-10010110001"; std::string str_auto = "0x7f"; std::string::size_type sz; // alias of size_t int i_dec = std::stoi (str_dec,&sz); int i_hex = std::stoi (str_hex,nullptr,16); int i_bin = std::stoi (str_bin,nullptr,2); int i_auto = std::stoi (str_auto,nullptr,0); std::cout << str_dec << ": " << i_dec << " and [" << str_dec.substr(sz) << "]\n"; std::cout << str_hex << ": " << i_hex << '\n'; std::cout << str_bin << ": " << i_bin << '\n'; std::cout << str_auto << ": " << i_auto << '\n'; return 0; }

输出

2001, A Space Odyssey: 2001 and [, A Space Odyssey] 40c3: 16579 -10010110001: -1201 0x7f: 127

其他类型 类似

无符号整型

stoul

如何实现整型与字符串之间的相互转换?

浮点型

stof

数值转字符串

std::string s;
s = std::to_string(1) + ” is int, “;

其他数值类型 类似

s = std::to_string(3.14f) + ” is float.”;

以上这篇C++ 整型与字符串的互转方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持自由互联。