參考了網上一些方法:所謂的短字符,就是用8bit來表示的字符,典型的應用是ASCII碼. 而寬字符,顧名思義,就是用16bit表示的字符,典型的有UNICODE.
常用的代碼頁有CP_ACP和CP_UTF8兩個。
使用CP_ACP代碼頁就實現了ANSI與Unicode之間的轉換。
使用CP_UTF8代碼頁就實現了UTF-8與Unicode之間的轉換。
1. ASCII to Unicode(CP_ACP)
std::wstring string2wstring_CP_ACP(std::string str) { std::wstring result = L""; int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), NULL, 0); TCHAR* buffer = new TCHAR[len + 1];//保存到Unicode串 MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), buffer, len); buffer[len] = '\0'; result.append(buffer); delete[] buffer; return result; }
2. Unicode to ASCII(CP_ACP)
std::string wstring2string_CP_ACP(std::wstring wstr) { std::string result = ""; int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL); char* buffer = new char [len + 1]; //保存ANSI串 WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL); buffer[len] = '\0'; result.append(buffer); delete[] buffer; return result; }
3. UTF-8 to Unicode(CP_UTF8)
std::wstring string2wstring_CP_UTF8(std::string str) { std::wstring result = L""; int len = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.size(), NULL, 0); TCHAR* buffer = new TCHAR[len + 1];//保存到Unicode串 MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.size(), buffer, len); buffer[len] = '\0'; result.append(buffer); delete[] buffer; return result; }
4. Unicode to UTF-8(CP_UTF8)
std::string wstring2string_CP_UTF8(std::wstring wstr) { std::string result = ""; int len = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL); char* buffer = new char [len + 1]; WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL); buffer[len] = '\0'; result.append(buffer); delete[] buffer; return result; }