最全的C/C++字符串与数值间的转换

---------------- C ----------------

< stdlib.h >

 *字符串转数值*
 /*注记:
 _Out_ 表示该函数真实输出,返回的int表示是否转换成功
 _Out_opt_ 一般用来保留转换信息之类的,这里的_EndPtr填NULL即可
 _Radix:转换进制,一般为2,8,10,16  */
 //int
 1.  int atoi (char const* _String);
 //long ~ unsigned long long
 2.  long atol (char const* _String);
 3.  long strtol (char const* _String,_Out_opt_  char**_EndPtr, int  _Radix);
 4.  unsigned long strtoul (char const* _String,_Out_opt_  char**_EndPtr, int  _Radix);
 5.  long long atoll  (char const* _String);
 6.  long long strtoll (char const* _String,_Out_opt_  char**_EndPtr, int  _Radix);
 7.  unsigned long long strtoull (char const* _String,_Out_opt_  char**_EndPtr, int  _Radix);
 //float
 8.  float strtof (char const* _String,_Out_opt_ char** _EndPtr);
 9.  int _atoflt (_Out_ float*  _Result, char const* _String); 
 //double ~ long double
 10. double atof   (char const* _String);
 11. double strtod (char const* _String,_Out_opt_ char**_EndPtr);
 12. int _atodbl (_Out_ double* _Result, char* _String);
 13. long double strtold (char const* _String,_Out_opt_ char**_EndPtr);
 14. int _atoldbl (_Out_ long double* _Result, char* _String);
 //__int64 为64位程序准备的
 15. __int64 _atoi64 (char const* _String);
 16. __int64 _strtoi64 (char const* _String,_Out_opt_  char**_EndPtr, int _Radix);
 17. unsigned __int64 _strtoui64 (char const* _String,_Out_opt_  char**_EndPtr, int  _Radix);
 *数值转字符串*
 18. char* _itoa (int_Value,char* _Buffer,int _Radix);
 19. char* _ltoa (long _Value,char* _Buffer,int _Radix);
 20. char* _ultoa (unsigned long _Value,char* _Buffer,int _Radix);
 22. char* _i64toa (__int64 _Value,char*_Buffer,int _Radix);
 23. char* _ui64toa (unsigned __int64 _Value,char*_Buffer,int _Radix);

< corecrt_wstdlib.h >


该头文件被 < stdlib.h > 、<string.h>等所包含,主要用来宽字符与数值之间的转换。

//形式大体是这样的,和上面的<stdlib.h>里的差不多
int _wtoi (wchar_t const* _String);
double wcstod (wchar_t const* _String,_Out_opt_ wchar_t**_EndPtr);

wchar_t* _itow (int _Value,_wchar_t* _Buffer,int _Radix);

< stdio.h >


*字符串转数值*
sscanf (char*Buffer,char* _Format,...);
//例如:char str[] = "123.567 89"; double a;int b;
// sscanf(str,"%lf%d",&a,&b);
*数值转字符串*
sprintf (char*Buffer,char* _Format,...);
//例如: char str[100]=""; sprintf(str,"%d %s",123,"456");

--------------- C++ ---------------

< string >

该头文件包含有<xstring>(里面是宽字符串wstring的定义)
下面函数重载有参数是 const wstring&*字符串转数值*
 1. int stoi(const string& _Str, size_t* _Idx = nullptr, int _Base = 10)
 2. long stol(const string& _Str, size_t* _Idx = nullptr, int _Base = 10)
 3. unsigned long stoul(const string& _Str, size_t* _Idx = nullptr, int _Base = 10)
 4. long long stoll(const string& _Str, size_t* _Idx = nullptr, int _Base = 10)
 5. unsigned long long stoull(const string& _Str, size_t* _Idx = nullptr, int _Base = 10)
 7. float stof(const string& _Str, size_t* _Idx = nullptr)
 8. double stod(const string& _Str, size_t* _Idx = nullptr)
 10.long double stold(const string& _Str, size_t* _Idx = nullptr)
 *数值转字符串*
 14. string to_string(int _Val)  参数还可以是uint -> long double
 15. wstring to_wstring(int _Val) 同上

< sstream >


可以借助sstream里的 stringstream 来进行字符串与数值转换。
stringstream 是一个重载了>>与<<类,可以向cin与cout一样使用。
要返回字符串时只需要 .str()即可,而要赋值给数值只需 >>value;

//例如:
stringstream sstream;
sstream<< 123 << "456";
string str = sstream.str();
int value ;
sstream >> value; //value = 123456;

//也可以写成函数来方便转换
template<typename T>
inline string NumToString(const T& value)
{
	stringstream sstream;
	sstream << value;
	return sstream.str();
}

template<typename T>
inline T StringToNum(const string& value)
{
	stringstream sstream;
	sstream << value;
	T temp;
	sstream >> temp;
	return temp;
}

< boost/lexical_cast.hpp >


需要先安装boost库才能使用,用法和其他_cast一样,十分方便。其函数定义为:
namespace boost{
template < typename Target >
inline Target lexical_cast(参数);
}

//用法:
using namespace boost;
string str = lexical_cast<string>(15.456);
wstring wstr = lexical_cast<wstring>(123);
double x = lexical_cast<double>(str);
int y = lexical_cast<int>(wstr);
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值