天天看點

字元串和數字之間的轉換(Unicode)

CString str ;

str = _T("1234");

int i = _ttoi(str);

float f = _tstof(str);

**************************

數字轉wchar_t

wchar_t c[10];

int num = 100;

_itow_s(num , c,10,10);

wstring str(c);

******************************

wstring to int

_wtoi(str.c_str());

******************************

如果你準備使用 Unicode 字元,你應該用_ttoi(),它在 ANSI 編碼系統中被編譯成_atoi(),而在 Unicode 編碼系統中編譯成_wtoi()。你也可以考慮使用_tcstoul()或者_tcstol(),它們都能把字元串轉化成任意進制的長整數(如二進制、八進制、十進制或十六進制),不同點在于前者轉化後的資料是無符号的(unsigned),而後者相反。看下面的例子:

CString hex = _T("FAB");

CString decimal = _T("4011");

ASSERT(_tcstoul(hex, 0, 16) == _ttoi(decimal));

轉載于:https://www.cnblogs.com/hbf369/archive/2012/01/06/2314634.html