本地数据临时存储,在string wstring CString CStringA char wchar tchar 之间转换

本文详细介绍了在C++中进行各种类型字符串(如CString,string,wstring,char,TCHAR等)之间的转换方法,特别关注了从WindowsANSI编码(CP_ACP)到更稳定的UTF-8编码的转换过程,强调了在永久存储时使用UTF-8或UTF-16的必要性,以避免因系统默认编码差异导致的数据损坏问题。
摘要由CSDN通过智能技术生成

一.概述与概念

这里做的转换都是本地数据(文字常量 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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值