C/C++ 字符串(string)转换
目錄
C語言字符串與基本數據類型互轉
C++?string與基本數據類型互轉
前言
本篇博文介紹C語言和C++中字符串與基本數據類型的轉換問題,在這之前要先了解以下byte string和string的區別。在很多情況下,不管是C語言還是C++,都會將一個字符序列”asdasd”統稱為字符串。但是在外國網站查看一些函數后就會發現,C的函數參數是byte string(在剛開始看的時候我還在糾結這個字節字符串是什么東西?其實它就是字符數組或者const char *),直接翻譯過來是字節字符串(或字節串)。而C++的函數參數是string,它是一個string類的對象。如有需要可以將一個string對象的內容轉換成const char *。
?
C語言字符串與基本數據類型互轉
字符串(字符數組)轉為各種基本數據類型
Defined in header <stdlib.h> 字符串轉整型 int?? ? ? atoi(?const?char?*str?); long?? ? ?atol(?const?char?*str?); long?long?atoll(?const?char?*str?);字符串轉浮點型 double atof( const char* str ); #include <stdio.h> #include <stdlib.h>int main(void) {printf("%i\n", atoi(" -123junk"));printf("%i\n", atoi("0"));printf("%i\n", atoi("junk")); // no conversion can be performedprintf("%i\n", atoi("2147483648")); // UB: out of range of int } 輸出: -123 0 0 -2147483648 #include <stdlib.h> #include <stdio.h>int main(void) {printf("%g\n", atof(" -0.0000000123junk"));printf("%g\n", atof("0.012"));printf("%g\n", atof("15e16"));printf("%g\n", atof("-0x1afp-2"));printf("%g\n", atof("inF"));printf("%g\n", atof("Nan"));printf("%g\n", atof("1.0e+309")); // UB: out of range of doubleprintf("%g\n", atof("0.0"));printf("%g\n", atof("junk")); // no conversion can be performed } 輸出: -1.23e-08 0.012 1.5e+17 -107.75 inf nan inf 0 0?
各種基本數據類型轉為字符串(字符數組)
(1)sprintf(buf, "%d" , value); 用于轉換帶符號的整數 (2)sprintf(buf, "%ld", value); 用于轉換帶符號的整數 (3)sprintf(buf, "%lld",value); 用于轉換帶符號的整數 (4)sprintf(buf, "%u", value); 用于轉換無符號整數 (5)sprintf(buf, "%lu", value); 用于轉換無符號整數 (6)sprintf(buf, "%llu",value); 用于轉換無符號整數 (7)sprintf(buf, "%f", value); 用于轉換float、double (8)sprintf(buf, "%Lf", value); 用于轉換long double #include <stdlib.h> #include <stdio.h>int main(void) {char buf[100];sprintf(buf, "%d", -12); printf("%c%c%c\n", buf[0], buf[1], buf[2]);sprintf(buf, "%ld", 12); printf("%s\n", buf); sprintf(buf, "%lld", 122222222222222); printf("%s\n", buf); sprintf(buf, "%u", 1);printf("%s\n", buf); sprintf(buf, "%lu", 111111111111); printf("%s\n", buf); sprintf(buf, "%llu", 2e18);printf("%s\n", buf); sprintf(buf, "%f", 12.8888);printf("%s\n", buf); sprintf(buf, "%Lf", 1.222e8);printf("%s\n", buf); } 輸出: -12 12 122222222222222 1 111111111111 4880707296814876672 12.888800 0.000000?
將字符串轉換為整數值
Defined in header <stdlib.h> <cstdlib> long?? ? ?strtol(?const?char?*str,?char?**str_end,?int?base?); long?long?strtoll(?const?char?*str,?char?**str_end,?int?base?);| str | - | pointer to the null-terminated byte string to be interpreted |
| str_end | - | pointer to a pointer to character. |
| base | - | base?of the interpreted integer value |
?
將字符串轉換為浮點型
Defined in header <stdlib.h> <cstdlib> float?? ? ? strtof(?const?char*?str,?char**?str_end?); double?? ? ?strtod(?const?char*?str,?char**?str_end?); long?double?strtold(?const?char*?str,?char**?str_end?);| str | - | pointer to the null-terminated byte string to be interpreted |
| str_end | - | pointer to a pointer to character. |
?
C++?string與基本數據類型互轉
string轉為整數
int?? ? ? stoi(?const?std::string&?str,?std::size_t*?pos?=?0,?int?base?=?10?); int?? ? ? stoi(?const?std::wstring&?str,?std::size_t*?pos?=?0,?int?base?=?10?); long?? ? ?stol(?const?std::string&?str,?std::size_t*?pos?=?0,?int?base?=?10?); long?? ? ?stol(?const?std::wstring&?str,?std::size_t*?pos?=?0,?int?base?=?10?); long?long?stoll(?const?std::string&?str,?std::size_t*?pos?=?0,?int?base?=?10?); long?long?stoll(?const?std::wstring&?str,?std::size_t*?pos?=?0,?int?base?=?10?);| str | - | the string to convert |
| pos | - | address of an integer to store the number of characters processed |
| base | - | the number base |
?
string轉為無符號long
unsigned?long?? ? stoul(?const?std::string&?str,?std::size_t*?pos?=?0,?int?base?=?10?); unsigned?long?? ? stoul(?const?std::wstring&?str,?std::size_t*?pos?=?0,?int?base?=?10?); unsigned?long?long?stoull(?const?std::string&?str,?std::size_t*?pos?=?0,?int?base?=?10?); unsigned?long?long?stoull(?const?std::wstring&?str,?std::size_t*?pos?=?0,?int?base?=?10?);| str | - | the string to convert |
| pos | - | address of an integer to store the number of characters processed |
| base | - | the number base |
?
string轉為浮點型
float?? ? ? stof(?const?std::string&?str,?std::size_t*?pos?=?0?); float?? ? ? stof(?const?std::wstring&?str,?std::size_t*?pos?=?0?); double?? ? ?stod(?const?std::string&?str,?std::size_t*?pos?=?0?); double?? ? ?stod(?const?std::wstring&?str,?std::size_t*?pos?=?0?); long?double?stold(?const?std::string&?str,?std::size_t*?pos?=?0?); long?double?stold(?const?std::wstring&?str,?std::size_t*?pos?=?0?);| str | - | the string to convert |
| pos | - | address of an integer to store the number of characters processed |
?
各類型轉string
Defined in header?<string> std::string?to_string(?int?value?); std::string?to_string(?long?value?); std::string?to_string(?long?long?value?); std::string?to_string(?unsigned?value?); std::string?to_string(?unsigned?long?value?); std::string?to_string(?unsigned?long?long?value?); std::string?to_string(?float?value?); std::string?to_string(?double?value?); std::string?to_string(?long?double?value?); #include <iostream> #include <string> int main() {double f = 23.43;double f2 = 1e-9;double f3 = 1e40;double f4 = 1e-40;double f5 = 123456789;std::string?f_str = std::to_string(f);std::string?f_str2 = std::to_string(f2); // Note: returns "0.000000"std::string?f_str3 = std::to_string(f3); // Note: Does not return "1e+40".std::string?f_str4 = std::to_string(f4); // Note: returns "0.000000"std::string?f_str5 = std::to_string(f5);std::cout?<< "std::cout: " << f << '\n'<< "to_string: " << f_str ?<< "\n\n"<< "std::cout: " << f2 << '\n'<< "to_string: " << f_str2 << "\n\n"<< "std::cout: " << f3 << '\n'<< "to_string: " << f_str3 << "\n\n"<< "std::cout: " << f4 << '\n'<< "to_string: " << f_str4 << "\n\n"<< "std::cout: " << f5 << '\n'<< "to_string: " << f_str5 << '\n'; }一個非常好的網站:?http://en.cppreference.com/w/cpp/string/basic_string/stol
總結
以上是生活随笔為你收集整理的C/C++ 字符串(string)转换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 海康威视Web端视频开发
- 下一篇: SSTable数据结构