目录
前言
本篇博文介绍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 performed
printf("%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 double
printf("%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 |
#include <iostream>
#include <string>
#include <cerrno>
#include <cstdlib>
#include <cstdio>
int main()
{
const char* p = "10 200000000000000000000000000000 30 -40";
char *end;
std::cout << "Parsing '" << p << "':\n";
for (long i = std::strtol(p, &end, 10);
p != end;
i = std::strtol(p, &end, 10))
{
std::cout << "'" << std::string(p, end-p) << "' -> ";
p = end;
if (errno == ERANGE){
std::cout << "range error, got ";
errno = 0;
}
std::cout << i << '\n';
}
// parsing without error handling
printf("\"1010\" in binary --> %ld\n", strtol("1010",NULL,2));
printf("\"12\" in octal --> %ld\n", strtol("12",NULL,8));
printf("\"A\" in hex --> %ld\n", strtol("A",NULL,16));
printf("\"junk\" in base-36 --> %ld\n", strtol("junk",NULL,36));
printf("\"012\" in auto-detected base --> %ld\n", strtol("012",NULL,0));
printf("\"0xA\" in auto-detected base --> %ld\n", strtol("0xA",NULL,0));
printf("\"junk\" in auto-detected base --> %ld\n", strtol("junk",NULL,0));
}
将字符串转换为浮点型
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. |
#include <iostream>
#include <string>
#include <cerrno>
#include <cstdlib>
int main()
{
const char* p = "111.11 -2.22 0X1.BC70A3D70A3D7P+6 1.18973e+4932zzz";
char* end;
std::cout << "Parsing \"" << p << "\":\n";
for (double f = std::strtod(p, &end); p != end; f = std::strtod(p, &end))
{
std::cout << "'" << std::string(p, end-p) << "' -> ";
p = end;
if (errno == ERANGE){
std::cout << "range error, got ";
errno = 0;
}
std::cout << f << '\n';
}
}
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 |
#include <iostream>
#include <string>
int main(){
std::string str1 = "45";
std::string str2 = "3.14159";
std::string str3 = "31337 with words";
std::string str4 = "words and 2";
int myint1 = std::stoi(str1);
int myint2 = std::stoi(str2);
int myint3 = std::stoi(str3);
// error: 'std::invalid_argument'
// int myint4 = std::stoi(str4);
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::cout << "std::stoi(\"" << str4 << "\") is " << myint4 << '\n';
}
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