C++ 在各种字符串类型之间相互转换

本文最后更新于:2022年3月23日 晚上

简述

最近在写 MFC 程序的过程中经常遇到不同字符串类型间的转换,也是在查资料的过程中意外发现了微软的一篇文档:如何:在各种字符串类型之间进行转换。介绍的相当全面,即便是没有提及的基本也可以通过多次转换得到,本文将比较常用的一些转换提炼成代码片段,以便以后查阅☠

多字节 <==> 宽字符

1. MultiByteToWideChar

官方文档:MultiByteToWideChar function (stringapiset.h)

int MultiByteToWideChar(
  UINT                              CodePage,
  DWORD                             dwFlags,
  _In_NLS_string_(cbMultiByte)LPCCH lpMultiByteStr,
  int                               cbMultiByte,
  LPWSTR                            lpWideCharStr,
  int                               cchWideChar
);

宽字符字符串(wchar_t * or wstring)转多字节字符串(string

std::string wc2mb(const wchar_t* buffer, int len)
{
	int nChars = WideCharToMultiByte(CP_UTF8, 0, buffer, len, NULL, 0, NULL, NULL);
	if (nChars == 0) return "";
	std::string str;
	str.resize(nChars);
	WideCharToMultiByte(CP_UTF8, 0, buffer, len, const_cast<char*>(str.c_str()), nChars, NULL, NULL);
	return str;
}

std::string wc2mb(const std::wstring& str)
{
	return wc2mb(str.c_str(), (int)str.size());
}

2. WideCharToMultiByte

官方文档:WideCharToMultiByte function (stringapiset.h)

int WideCharToMultiByte(
  UINT                               CodePage,
  DWORD                              dwFlags,
  _In_NLS_string_(cchWideChar)LPCWCH lpWideCharStr,
  int                                cchWideChar,
  LPSTR                              lpMultiByteStr,
  int                                cbMultiByte,
  LPCCH                              lpDefaultChar,
  LPBOOL                             lpUsedDefaultChar
);

多字节字符串(char *)转宽字符字符串(wchar_t *

wchar_t* mb2wc(char * chs)
{
	int bufSize = MultiByteToWideChar(CP_UTF8, 0, chs, -1, NULL, 0);
	wchar_t* wcs = new wchar_t[bufSize];
	MultiByteToWideChar(CP_UTF8, 0, chs, -1, wcs, bufSize);
	return wcs;
}

char * 转换

1. To wchar_t *

char *orig = "Hello, World!";
size_t newsize = strlen(orig) + 1;  // 14
wchar_t * wcstring = new wchar_t[newsize];
size_t convertedChars = 0;
mbstowcs_s(&convertedChars, wcstring, newsize, orig, _TRUNCATE);  // Required <stdlib.h>

2. To _bstr_t

char *orig = "Hello, World!";
_bstr_t bstrt(orig);

3. To CComBSTR

char *orig = "Hello, World!";
CComBSTR ccombstr(orig);
CW2A printstr(ccombstr);
cout << printstr << endl;

4. To CStringA

char *orig = "Hello, World!";
CStringA cstringa(orig);

5. To CStringW

char *orig = "Hello, World!";
CStringW cstring(orig);

6. To string

char *orig = "Hello, World!";
string basicstring(orig);

7. To System::String

char *orig = "Hello, World!";
String ^systemstring = gcnew String(orig);
delete systemstring;

wchar_t * 转换

1. To char *

wchar_t *orig = _T("Hello, World!");
size_t origsize = wcslen(orig) + 1;  // 14
size_t newsize = origsize * 2;  // 28, 宽字符占2个字节
char *nstring = new char[origsize];
size_t convertedChars = 0;
wcstombs_s(&convertedChars, nstring, newsize, orig, _TRUNCATE);  // Put a copy of the converted string into nstring
_mbscat_s((unsigned char*)nstring, newsize, (unsigned char*)strConcat);  // append the type of string to the new string.

2. To _bstr_t

wchar_t *orig = _T("Hello, World!");
_bstr_t bstrt(orig);

3. To CComBSTR

wchar_t *orig = _T("Hello, World!");
CComBSTR ccombstr(orig);
CW2A printstr(ccombstr);
cout << printstr << endl;
wcout << (LPCTSTR) ccombstr << endl;  // 更简单

4. To CStringA

wchar_t *orig = _T("Hello, World!");
CStringA cstringa(orig);

5. To CStringW

wchar_t *orig = _T("Hello, World!");
CStringW cstring(orig);

6. To wstring

wchar_t *orig = _T("Hello, World!");
wstring basicstring(orig);

7. To System::String

wchar_t *orig = _T("Hello, World!");
String ^systemstring = gcnew String(orig);
delete systemstring;

CString 转换

1. CStringA to char *

CStringA origa("Hello, World!");
const size_t newsizea = (origa.GetLength() + 1);
char *nstringa = new char[newsizea];
strcpy_s(nstringa, newsizea, origa);

2. CStringW to char *

CStringW origw("Hello, World!");
const size_t newsizew = (origw.GetLength() + 1) * 2;
char *nstringw = new char[newsizew];
size_t convertedCharsw = 0;
wcstombs_s(&convertedCharsw, nstringw, newsizew, origw, _TRUNCATE );

3. CStringA to wchar_t *

CStringA origa("Hello, World!");
const size_t newsizea = (origa.GetLength() + 1);
size_t convertedCharsa = 0;
wchar_t *wcstring = new wchar_t[newsizea];
mbstowcs_s(&convertedCharsa, wcstring, newsizea, origa, _TRUNCATE);

4. CStringW to wchar_t *

CStringW origw("Hello, World!");
const size_t newsizew = (origw.GetLength() + 1) * 2;
wchar_t *n2stringw = new wchar_t[newsizew];
wcscpy_s( n2stringw, newsizew, origw );

5. CStringA to _bstr_t

CStringA origa("Hello, World!");
_bstr_t bstrt(origa);

6. CStringW to _bstr_t

CStringW origw("Hello, World!");
bstr_t bstrtw(origw);

7. CStringA to CComBSTR

CStringA origa("Hello, World!");
CComBSTR ccombstr(origa);
CW2A printstr(ccombstr);
cout << printstr << endl;

8. CStringW to CComBSTR

CStringW origw("Hello, World!");
CComBSTR ccombstrw(origw);
CW2A printstrw(ccombstrw);
wcout << printstrw << endl;

9. CStringA to string

CStringA origa("Hello, World!");
string basicstring(origa);

10. CStringW to wstring

CStringW origw("Hello, World!");
wstring basicstringw(origw);

11. CStringA to System::String

CStringA origa("Hello, World!");
String ^systemstring = gcnew String(origa);
delete systemstring;

12. CStringA to System::String

CStringW origw("Hello, World!");
String ^systemstringw = gcnew String(origw);
delete systemstringw;

从 basic_string 转换

1. string to char *

string orig("Hello, World!");
const size_t newsize = (strlen(orig.c_str()) + 1)*2;
char *nstring = new char[newsize];
strcpy_s(nstring, newsize, orig.c_str());

2. string to wchar_t *

string orig("Hello, World!");
const size_t newsizew = strlen(orig.c_str()) + 1;
size_t convertedChars = 0;
wchar_t *wcstring = new wchar_t[newsizew];
mbstowcs_s(&convertedChars, wcstring, newsizew, orig.c_str(), _TRUNCATE);

3. wstring to wchar_t *

wstring origw(L"Hello, World!");
wchar_t *wcstring = origw.c_str();

4. string to _bstr_t

string orig("Hello, World!");
_bstr_t bstrt(orig.c_str());

5. string to CComBSTR

string orig("Hello, World!");
CComBSTR ccombstr(orig.c_str());
CW2A printstr(ccombstr);
cout << printstr << endl;

6. string to CStringA

string orig("Hello, World!");
CStringA cstring(orig.c_str());

7. string to CStringW

string orig("Hello, World!");
CStringW cstringw(orig.c_str());

8. string to System::String

string orig("Hello, World!");
String ^systemstring = gcnew String(orig.c_str());
delete systemstring;

参考文档


C++ 在各种字符串类型之间相互转换
https://mxy493.xyz/2021030220973/
作者
mxy
发布于
2021年3月2日
许可协议