c++ 字符串转数字或数字转字符串

  在C++中字符串转换为数字,或数字转换为字符串,用到如下函数:

  _itoa atoi、atof、itoa、itow _itoa_s

1.整形转换为字符串:

  • wchar_t * _itot(int _Value,wchar_t _Dest,int _Radix);//第一个参数为想要转换的那个整形,第二参数为你的目的字符串,第三个为进制(填10则转换出来的是10进制)

2.字符串转为整形:

  • int _ttoi(const wchar_t *Str);//直接把需要转换的字符串作为参数,返回的就是整形了

 在字符集设置不同下会有不同的类型,说白了,这几个函数的功能都相同,但是根据你的字符集不同,选用的函数也不同。
_itot 在ASICII下被宏定义成_itoa,也就是说你调用_itot,实际上就是调用了_itoa,同理,在Unicode下就是_itow了。其实就是和TCHAR 一样,在Unicode下是WCHAR 而ASCII下就是char了。

//#include<tchar.h>  
//_itot例子  
int number = 1320;  
TCHAR trans[5];  
_itot(number, trans, 10);//调用trans,得出该字符串里为“1320”。如果第三个参数为2,则会输出1320的二进制,即10100101000。请注意数组越界的问题  
  
//_ttoi例子  
TCHAR trans[5] = L"321";  
int number = _ttoi(trans);//此时number为321  

源贴地址:http://blog.csdn.net/igaming/article/details/21330661

原文地址:https://www.cnblogs.com/happinessday/p/6380988.html