C++中常見類型轉換自定義函數

        遇到什麼寫什麼吧,以後再做補充。

1、int轉string

string intToString(const int n)	//需包含頭文件:#include <sstream>
{
	stringstream newstr;	
	newstr<<n;
	return newstr.str();
}

2、WORD轉string

string WORDToString(WORD w)
{
    char tmpbuff[16];
    sprintf(tmpbuff,"%d",w);
    string res=tmpbuff;
    return res;
}
注:typedef unsigned short      WORD; 所以也可以當做int類型,使用上面的intToString()進行轉換。

3、無符號整型(unsigned int)轉string

        注:無符號整型還是整型,所以仍然可以用方法1。當然你也可以硬着頭皮寫一個函數,比如這哥們:http://blog.csdn.net/koala0923/article/details/9964897

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章