Thứ Tư, 15 tháng 5, 2013

[C/C++] Cross platform - Convert std::string to std::wstring and vice versa - conver string and wstring to int/long

This is one of the easiest way to convert a std::string to std::wstring and vice versa:

1. Convert string to wstring:

std::string myString = " This is test string";
std::wstring wideString( myString.begin(), myString.end());

or

std::string myString = " This is test string";
std::wstring wideString;
wideString.assign( myString.begin(), myString.end());

2. Convert wstring to string - Similar to converting string to wstring


std::string wideString= L" This is wide string";
std::wstring asciiString( wideString.begin(), wideString.end());

or


std::string wideString= L" This is test string";
std::wstring asciiString;
asciiString.assign( wideString.begin(), wideString.end());

3. convert wstring to integer
int myInt;
wstring myString;

myInt = wcstol( myString.c_str(), NULL, 10);

read more about this function here: http://www.cplusplus.com/reference/cwchar/wcstol/.



Không có nhận xét nào:

Đăng nhận xét