《数字、string、wstring之间的相互转换》

使用C++版本

C++11及更高版本

string和wstring的区别

wstring(wchar_t string)是宽char,Unicode编码,一般情况下一个字符占两个字节大小
string(char string)是窄char,AscII编码,一个字符占一个字节大小

string转数字

1、string转int

函数原型
int stoi (const string&  str, size_t* idx = 0, int base = 10);
int stoi (const wstring& str, size_t* idx = 0, int base = 10);

参数解析

str:

String object with the representation of an integral number.(用整数表示的字符串对象)

idx:
Pointer to an object of type size_t, whose value is set by the function to position of the next character in str after the numerical value.This parameter can also be a null pointer, in which case it is not used.(指向类型size_t的对象的指针,该对象的值由函数设置为在数值后的str中下一个字符的位置。这个参数也可以是一个空指针,在这种情况下不使用它。)
base:

Numerical base (radix) that determines the valid characters and their interpretation.If this is 0, the base used is determined by the format in the sequence (see strtol for details). Notice that by default this argument is 10, not 0.(确定有效字符及其解释的数字基数(基数)。如果这是0,那么所使用的基数由序列中的格式决定(详细信息请参见strtol)。注意,默认情况下这个参数是10,而不是0。)

微软官方示例代码
// stoi example
#include <iostream>   // std::cout
#include <string>     // std::string, std::stoi

int main ()
{
	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';
	system("pause");
	return 0;
}
输出结果


2、string转int

函数原型
long stol (const string&  str, size_t* idx = 0, int base = 10);
long stol (const wstring& str, size_t* idx = 0, int base = 10);
参数解析

str:
String object with the representation of an integral number.(用整数表示的字符串对象。)
idx:
Pointer to an object of type size_t, whose value is set by the function to position of the next character in str after the numerical value.(指向类型size_t的对象的指针,该对象的值由函数设置为在数值后的str中下一个字符的位置。)
This parameter can also be a null pointer, in which case it is not used.
base:
Numerical base (radix) that determines the valid characters and their interpretation.
If this is 0, the base used is determined by the format in the sequence (see strtol for details). Notice that by default this argument is 10, not 0.(确定有效字符及其解释的数字基数(基数)。如果这是0,那么所使用的基数由序列中的格式决定(详细信息请参见strtol)。注意,默认情况下这个参数是10,而不是0。)

微软官方示例代码
// stol example
#include <iostream>   // std::cout
#include <string>     // std::string, std::stol

int main ()
{
  std::string str_dec = "1987520";
  std::string str_hex = "2f04e009";
  std::string str_bin = "-11101001100100111010";
  std::string str_auto = "0x7fffff";

  std::string::size_type sz;   // alias of size_t

  long li_dec = std::stol (str_dec,&sz);
  long li_hex = std::stol (str_hex,nullptr,16);
  long li_bin = std::stol (str_bin,nullptr,2);
  long li_auto = std::stol (str_auto,nullptr,0);

  std::cout << str_dec << ": " << li_dec << '\n';
  std::cout << str_hex << ": " << li_hex << '\n';
  std::cout << str_bin << ": " << li_bin << '\n';
  std::cout << str_auto << ": " << li_auto << '\n';

  return 0;
}
输出结果

3、string转unsigned long

函数原型
unsigned long stoul (const string&  str, size_t* idx = 0, int base = 10);
unsigned long stoul (const wstring& str, size_t* idx = 0, int base = 10);
参数解析
str:
String object with the representation of an integral number.
idx:
Pointer to an object of type size_t, whose value is set by the function to position of the next character in str after the numerical value.
This parameter can also be a null pointer, in which case it is not used.
base:
Numerical base (radix) that determines the valid characters and their interpretation.

If this is 0, the base used is determined by the format in the sequence (see strtol for details). Notice that by default this argument is 10, not 0.

微软官方示例代码
// stoul example
#include <iostream>   // std::cin, std::cout
#include <string>     // std::string, std::stoul, std::getline

int main ()
{
	std::string str;
	std::cout << "Enter an unsigned number: ";
	std::getline (std::cin,str);
	unsigned long ul = std::stoul (str,nullptr,0);
	std::cout << "You entered: " << ul << '\n';
	system("pause");
	return 0;
}
输出结果

4、string转long long

5、string转unsigned long long

6、string转float

7、string转double

8、string转long double

数字string

数字转string类型统一使用to_string()函数进行转换,我的只支持convert long long to string、convert unsigned long long to string、convert long double to string这三种,可能是我的C++版本低了点,一般报错“参数不匹配”,注意更换,解决问题。

错误	1	error C2668: “std::to_string”: 对重载函数的调用不明确	
	3	IntelliSense: 有多个 重载函数 "to_string" 实例与参数列表匹配:	
函数原型
string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);
示例代码
// to_string example
#include <iostream>   // std::cout
#include <string>     // std::string, std::to_string
using namespace std;
int main ()
{
	std::string pi = "pi is " + to_string((long double)3.1415926);
	std::string perfect = to_string((long long)1+2+4+7+14) + " is a perfect number";
	std::cout << pi << '\n';
	std::cout << perfect << '\n';
	system("pause");
	return 0;
}

wstring转数字

1、convert wstring to int

stoi

2、convert wstring to long

stol

3、convert wstring to unsigned long

stoul

4、convert wstring to long long

stoll

5、convert wstring to unsigned long long

stoull

6、convert wstring to float

stof

7、convert wstring to double

stod

8、convert wstring to long double

stold

数字转wstring

数字转wstring类型统一使用to_wstring()函数进行转换,跟to_string一样的用法,同样的,我的也只支持convert long long to string、convert unsigned long long to string、convert long double to string这三种,可能是我的C++版本低了点,一般报错“参数不匹配”,注意更换,解决问题。

函数原型
wstring to_wstring (int val);
wstring to_wstring (long val);
wstring to_wstring (long long val);
wstring to_wstring (unsigned val);
wstring to_wstring (unsigned long val);
wstring to_wstring (unsigned long long val);
wstring to_wstring (float val);
wstring to_wstring (double val);
wstring to_wstring (long double val);
示例代码
// to_wstring example
#include <iostream>   // std::wcout
#include <string>     // std::wstring, std::to_wstring
using namespace std;
int main ()
{
	std::wstring pi = L"pi is " + to_wstring((long double)3.1415926);
	std::wstring perfect = to_wstring((long long)1+2+4+7+14) + L" is a perfect number";
	std::wcout << pi << L'\n';
	std::wcout << perfect << L'\n';
	system("pause");
	return 0;
}


猜你喜欢

转载自blog.csdn.net/mars_xiaolei/article/details/80855587