数字转字符串:
用C++的streanstream:
![](http://hiphotos.baidu.com/mycral/pic/item/22c59225682937524d088d9f.jpg)
#include <sstream>
![](http://hiphotos.baidu.com/mycral/pic/item/856cd2009eb8f0551c95839f.jpg)
#Include <string>
![](http://hiphotos.baidu.com/mycral/pic/item/71fd1507c92594280308819f.jpg)
string num2str(double i)
![](http://hiphotos.baidu.com/mycral/pic/item/2060717ad4b8e5a70bd1879f.jpg)
{
![](http://hiphotos.baidu.com/mycral/pic/item/3ffd00f5d261fc8af3d3859f.jpg)
stringstream ss;
![](http://hiphotos.baidu.com/mycral/pic/item/372419d82863c97f10df9b9f.jpg)
ss<<i;
![](http://hiphotos.baidu.com/mycral/pic/item/cf262c2dcd6f37bf8b13999f.jpg)
return ss.str();
![](http://hiphotos.baidu.com/mycral/pic/item/2c2ad2ed54a3fd7463d09f9f.jpg)
}
字符串转数字:
![](http://hiphotos.baidu.com/mycral/pic/item/b7e6182682606950908f9d9f.jpg)
int str2num(string s)
![](http://hiphotos.baidu.com/mycral/pic/item/5f258c02733f3ebf09fa939f.jpg)
{
![](http://hiphotos.baidu.com/mycral/pic/item/ac7adbedec4acf6e2797919f.jpg)
int num;
![](http://hiphotos.baidu.com/mycral/pic/item/350f2a3cc027c7bd3c6d979f.jpg)
stringstream ss(s);
![](http://hiphotos.baidu.com/mycral/pic/item/1b6222efd5ddcba8b3fb959f.jpg)
ss>>num;
![](http://hiphotos.baidu.com/mycral/pic/item/00982efa584bdd456c22eb9f.jpg)
return num;
![](http://hiphotos.baidu.com/mycral/pic/item/8f0e38178192d11921a4e99f.jpg)
}
上面方法很简便, 缺点是处理大量数据转换速度较慢..
C library中的sprintf, sscanf 相对更快
可以用sprintf函数将数字输出到一个字符缓冲区中. 从而进行了转换...
例如:
已知从0点开始的秒数(seconds) ,计算出字符串"H:M:S", 其中H是小时, M=分钟,S=秒
![](http://hiphotos.baidu.com/mycral/pic/item/50d7344bce14d87008f7ef9f.jpg)
int H, M, S;
![](http://hiphotos.baidu.com/mycral/pic/item/1d513d22f9470e019922ed9f.jpg)
string time_str;
![](http://hiphotos.baidu.com/mycral/pic/item/3402eb536a92ec5d0df3e39f.jpg)
H=seconds/3600;
![](http://hiphotos.baidu.com/mycral/pic/item/a5d7090ff84349997acbe19f.jpg)
M=(seconds%3600)/60;
![](http://hiphotos.baidu.com/mycral/pic/item/3106accb8d7b9415f31fe79f.jpg)
S=(seconds%3600)%60;
![](http://hiphotos.baidu.com/mycral/pic/item/463e71470aaf205a6a63e59f.jpg)
char ctime[10];
![](http://hiphotos.baidu.com/mycral/pic/item/cfeac50891d368776a60fb9f.jpg)
sprintf(ctime, "%d:%d:%d", H, M, S); // 将整数转换成字符串
![](http://hiphotos.baidu.com/mycral/pic/item/56968d2597d092608644f99f.jpg)
time_str=ctime; // 结果
与sprintf对应的是sscanf函数, 可以将字符串转换成数字
![](http://hiphotos.baidu.com/mycral/pic/item/5695773279f475ab1b4cff9f.jpg)
char str[] = "15.455";
![](http://hiphotos.baidu.com/mycral/pic/item/bab190f91afdcf5cd8f9fd9f.jpg)
int i;
![](http://hiphotos.baidu.com/mycral/pic/item/e40cac2b64e8bb7ed52af19f.jpg)
float fp;
![](http://hiphotos.baidu.com/mycral/pic/item/5dac5e2cd29bb667359bf79f.jpg)
sscanf( str, "%d", &i ); // 将字符串转换成整数 i = 15
![](http://hiphotos.baidu.com/mycral/pic/item/e9df53353c2a9d685ab5f59f.jpg)
sscanf( str, "%f", &fp ); // 将字符串转换成浮点数 fp = 15.455000
![](http://hiphotos.baidu.com/mycral/pic/item/096e783a510457b914cecb9f.jpg)
//打印
![](http://hiphotos.baidu.com/mycral/pic/item/d4d1c39878b1d25b6f068c9f.jpg)
printf( "Integer: = %d ", i+1 );
![](http://hiphotos.baidu.com/mycral/pic/item/6640b2eb197fa880d439c99f.jpg)
printf( "Real: = %f ", fp+1 );
![](http://hiphotos.baidu.com/mycral/pic/item/283b4dd2db8828533bf3cf9f.jpg)
return 0;
输出如下:
Integer: = 16
Real: = 16.455000
另外
mfc里面还有CString format函数,把数字转成字符串。
C语言里还有什么 atoi itoa _atow ....