string str;
cstring cstr(str.c_str());
string str;
char* ch;
ch = str.c_str();
string str;
double dou;
dou = atof(str.c_str());
string str;
int i;
i = std::atoi(str.c_str());
cstring cstr;
char* ch;
//不出現亂碼
ch = cstr.getbuffer(0);
// 出現亂碼,還需將wchar_t*轉為char*
ch = tonarrowstring(cstr.getbuffer(0));
cstring cstr;
string str = cstringa(cstr);
cstring cstr;
int i;
cstr.format(」%d」, i);
cstring cstr;
// %d轉int
cstr.format(_t(「%d」),temp);
// %f轉float,double
cstr.format(_t(「%f」),temp);
// %s轉string
cstr.format(_t(「%s」),temp);
char* ch;
string str;
str = ch;
char* ch;
cstring cstr(ch);
//轉寬字元的處理
wchar_t* towidestring(const char* pstr)
std::wstring towidestring(const std::string& str)
wchar_t* wcret = towidestring(str.c_str());
wstring ret(wcret);
delete wcret;
return ret; }
//轉窄字元的處理
char* tonarrowstring(const wchar_t* pstr)
std::string tonarrowstring(const std::wstring& str)
char* cret = tonarrowstring(str.c_str());
string ret(cret);
delete cret;
return ret ; }
//itoa(int, char ,
radix);
// radix
為進製
char
ch[10];
int num = 100;
string str;
str = itoa(num, ch , 10);
long lon;
double dou;
dou = (double)lon;
double dou;
long lon;
lon = (long)dou;
double dou;
c 貨幣
2.5.tostring("c")
¥2.50
d 十進位制數
25.tostring("d5")
00025
e 科學型
25000.tostring("e")
2.500000e+005
f 固定點
25.tostring("f2")
25.00
g 常規……
wstring string_utf8tounicode(const char* src, std::wstring &t)
int size_s = strlen(src);
int size_d = size_s + 10; //?
wchar_t *des = new wchar_t[size_d];
memset(des, 0, size_d * sizeof(wchar_t));
int s = 0, d = 0;
//bool toomuchbyte = true; //set true to skip error prefix.
while (s < size_s && d < size_d)
else if((c & 0xe0) == 0xc0) ///< 110x-***x 10xx-***x
else if((c & 0xf0) == 0xe0) ///< 1110-***x 10xx-***x 10xx-***x
else if((c & 0xf8) == 0xf0) ///< 1111-0*** 10xx-***x 10xx-***x 10xx-***x
else
} t = des;
delete des;
des = null;
return t; }
string string_utf8toansi(const char *strutf)
int string_unicodetoutf8( const wstring& strres, char *utf8, int nmaxsize )
int len = 0;
int size_d = nmaxsize;
for (wstring::const_iterator it = strres.begin(); it != strres.end(); ++it)
else if(wchar < 0x800)
else if(wchar < 0x10000 )
else if( wchar < 0x200000 )
} return len; }
char *string_ansitoutf8(const string& strsrc)
//strres = chutf8;
//delete chutf8;
return chutf8; }
string string_halftofull(string str)
return string_unicodetoansi(ws); }
資料型別轉換 總結
語法 cast expression as data type length convert data type length expression style 引數 expression 任何有效的表示式。data type 作為目標的系統提供資料型別。length 目標資料型別的長度 目標資料型...
C 資料型別轉換
轉cstring cstring.format t d int 轉char 1.itoa int,char 10 10為十進位制 沒有越界檢查 2.memset szbuf,0,sizeof szbuf snprintf szbuf,sizeof szbuf d int 轉tchar itoa in...
c 資料型別轉換
隱式型別轉換 這些轉換是 c 預設的以安全方式進行的轉換,不會導致資料丟失。例如,從小的整數型別轉換為大的整數型別,從派生類轉換為基類。轉換規則從儲存範圍小的型別到儲存範圍大的型別。整數具體規則為 byte short char int long float double也就是說byte型別的變數可...