一.概述与概念
这里做的转换都是本地数据(文字常量 ANSI)转换所以CA2W/CW2A的第二个参数都是CP_ACP,
如果源数据是别的编码,第二个参数请换成别的,比如CP_UTF8(UTF8)就常用与网络传输,永久存储
CP_ACP:
系统默认的Windows ANSI代码页。 注意这个值在不同的计算机上可能不同,甚至在同一个网络上。它可以在同一台计算机上更改,导致存储的数据变得不可恢复的损坏。此值仅用于临时使用,永久存储应尽可能使用UTF-16或UTF-8。 |
其他参考:WideCharToMultiByte function (stringapiset.h) - Win32 apps | Microsoft Learn
二.转换
1.CString 2 其他
#pragma warning(disable:4996)
#define _CRT_SECURE_NO_WARNINGS
#include <afx.h>
#include <Windows.h>
#include <string>
#include <tchar.h>
#include <wchar.h>
#include <stdio.h>
using namespace std;
HMODULE g_hLogModule = NULL;
//HMODULE g_hLogModule = NULL;
int main()
{
CString cstrText = L"我是测试使用的小数据L";
CStringA cstraText;
string strText;
wstring wstrText;
wchar_t wzText[MAX_PATH] = { 0 };
char szText[MAX_PATH] = { 0 };
TCHAR tzText[MAX_PATH] = { 0 };
//CString 2 CStringA
cstraText = CW2A(cstrText.GetBuffer(), CP_ACP).m_psz;
//CString 2 wstring
wstrText = cstrText.GetBuffer();
//CString 2 wchar
wsprintf(wzText, L"%s", cstrText.GetBuffer());
//CString 2 char
sprintf(szText, "%s", CW2A(cstrText.GetBuffer(), CP_ACP).m_psz);
//wstring 2 tchar
_stprintf(tzText, _T("%s"), cstrText.GetBuffer());
return 0;
}
2.string 2 其他
#pragma warning(disable:4996)
#define _CRT_SECURE_NO_WARNINGS
#include <afx.h>
#include <Windows.h>
#include <string>
#include <tchar.h>
#include <wchar.h>
#include <stdio.h>
using namespace std;
HMODULE g_hLogModule = NULL;
//HMODULE g_hLogModule = NULL;
int main()
{
CString cstrText;
string strText = "我是测试使用的小数据L";
wstring wstrText;
wchar_t wzText[MAX_PATH] = { 0 };
char szText[MAX_PATH] = { 0 };
TCHAR tzText[MAX_PATH] = { 0 };
CStringA cstraText;
//string 2 CStringA
cstraText = strText.c_str();
//string 2 CString
cstrText = CA2W(strText.c_str(), CP_ACP).m_psz;
//string 2 wstring
wstrText = CA2W(strText.c_str(), CP_ACP).m_psz;
//string 2 wchar
wsprintf(wzText, L"%s", CA2W(strText.c_str(), CP_ACP).m_psz);
//string 2 char
sprintf(szText, "%s", strText.c_str());
//string 2 tchar
_stprintf(tzText, L"%s", CA2T(strText.c_str(), CP_ACP).m_psz);
return 0;
}
3.wstring 2 其他
#pragma warning(disable:4996)
#define _CRT_SECURE_NO_WARNINGS
#include <afx.h>
#include <Windows.h>
#include <string>
#include <tchar.h>
#include <wchar.h>
#include <stdio.h>
using namespace std;
HMODULE g_hLogModule = NULL;
//HMODULE g_hLogModule = NULL;
int main()
{
CString cstrText;
CStringA cstraText;
string strText;
wstring wstrText = L"我是测试使用的小数据L";
wchar_t wzText[MAX_PATH] = { 0 };
char szText[MAX_PATH] = { 0 };
TCHAR tzText[MAX_PATH] = { 0 };
//wstring 2 CStringA
cstraText = CW2A(wstrText.c_str(), CP_ACP).m_psz;
//wstring 2 CString
cstrText = wstrText.c_str();
//wstring 2 string
strText = CW2A(wstrText.c_str(), CP_ACP).m_psz;
//wstring 2 wchar
wsprintf(wzText, L"%s", wstrText.c_str());
//wstring 2 char
sprintf(szText, "%s", CW2A(wstrText.c_str(), CP_ACP).m_psz);
//wstring 2 tchar
_stprintf(tzText, _T("%s"), wstrText.c_str());
return 0;
}
4.WCHAR 2 其他
#pragma warning(disable:4996)
#define _CRT_SECURE_NO_WARNINGS
#include <afx.h>
#include <Windows.h>
#include <string>
#include <tchar.h>
#include <wchar.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
HMODULE g_hLogModule = NULL;
int main()
{
CString cstrText;
CStringA cstraText;
string strText;
wstring wstrText;
wchar_t wzText[MAX_PATH] = L"我是测试使用的小数据L";
char szText[MAX_PATH] = { 0 };
TCHAR tzText[MAX_PATH] = { 0 };
//wchar 2 CStringA
cstraText = CW2A(wzText, CP_ACP).m_psz;
//wchar 2 CString
cstrText = wzText;
//wchar 2 string
strText = CW2A(wzText, CP_ACP).m_psz;
//wchar 2 wstring
wstrText = wzText;
//wchar 2 char
sprintf(szText, "%s", CW2A(wzText, CP_ACP).m_psz);
//wchar 2 tchar
_stprintf(tzText, _T("%s"), CW2T(wzText,CP_ACP).m_psz);
return 0;
}
5.char 2 其他
#pragma warning(disable:4996)
#define _CRT_SECURE_NO_WARNINGS
#include <afx.h>
#include <Windows.h>
#include <string>
#include <tchar.h>
#include <wchar.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main()
{
CString cstrText;
CStringA cstraText;
string strText;
wstring wstrText;
wchar_t wzText[MAX_PATH] = { 0 };
char szText[MAX_PATH] = "我是测试使用的小数据L";
TCHAR tzText[MAX_PATH] = { 0 };
//char 2 CStringA
cstraText = szText;
//char 2 CString
cstrText = CA2W(szText, CP_ACP).m_psz;
//char 2 string
strText = szText;
//char 2 wstring
wstrText = CA2W(szText, CP_ACP).m_psz;
//char 2 wchar
wsprintf(wzText, L"%s", CA2W(szText, CP_ACP).m_psz);
//wchar 2 tchar
_stprintf(tzText, _T("%s"), CA2T(szText, CP_ACP).m_psz);
return 0;
}
6.tchar 2 其他
#pragma warning(disable:4996)
#define _CRT_SECURE_NO_WARNINGS
#include <afx.h>
#include <Windows.h>
#include <string>
#include <tchar.h>
#include <wchar.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main()
{
CString cstrText;
CStringA cstraText;
string strText;
wstring wstrText;
wchar_t wzText[MAX_PATH] = { 0 };
char szText[MAX_PATH] = {0};
TCHAR tzText[MAX_PATH] = _T("我是测试使用的小数据L");
//tchar 2 CStringA
cstraText = CT2A(tzText, CP_ACP).m_psz;
//tchar 2 CString
cstrText = CT2W(tzText, CP_ACP).m_psz;
//tchar 2 string
strText = CT2A(tzText, CP_ACP).m_psz;;
//tchar 2 wstring
wstrText = CT2W(tzText, CP_ACP).m_psz;
//tchar 2 char
sprintf(szText, "%s", CT2A(tzText, CP_ACP).m_psz);
//tchar 2 wchar
wsprintf(wzText, L"%s", CT2W(tzText, CP_ACP).m_psz);
return 0;
}