#include<iostream>
#include<windows.h>
#include<atlstr.h>
#include <locale.h>
#include<string>
using std::wstring;
using std::string;
using std::cout;
using std::wcout;
using std::endl;
void MultiByte2Wide(char* pSrc, wchar_t *pRst);
void Wide2MultiByte(wchar_t *pSrc, char* pRst);
void Utf2MultiByte(char *pSrc, char *pRst);
void MultiByte2Utf8(char *pSrc, char *pRst);
int main()
{
wcout.imbue(std::locale("chs"));//输出含有汉字的宽字符
//setlocale(LC_ALL, "");//输出wchar_t
//宽字节转多字节
wchar_t wszData[] = L"你好啊";
wcout << wszData << endl;
char pData[20] = { 0 };
Wide2MultiByte(wszData, pData);
cout << pData << endl;
//多字节转宽字节
wchar_t wsaRst[20] = { 0 };
MultiByte2Wide(pData, wsaRst);
wcout << wsaRst << endl;
//UTF8转多字节
char szData[20] = "helee";
Utf2MultiByte(szData, pData);
cout << pData << endl;
//CString转string
CString cstrData(L"在?");
string strData = (CW2A)cstrData;
wcout << cstrData.GetBuffer() << endl;
//string转CString
cstrData = (CA2W)strData.c_str();
//wstring转string
wstring wstrData(L"在干嘛呢");
strData = (CW2A)wstrData.c_str();
cout << strData << endl;
//char转CString
CString cstrRst = szData;
wcout << cstrRst.GetBuffer() << endl;
//CString 转char
ZeroMemory(szData, 10);
sprintf_s(szData, "%S", cstrRst.GetBuffer());
cout << szData << endl;
system("pause");
return 0;
}
//多字节转宽字节
void MultiByte2Wide(char * pSrc, wchar_t *pRst)
{
int nLen = MultiByteToWideChar(CP_ACP, 0, pSrc, -1, NULL, 0);
wchar_t *pWstr = new wchar_t[(nLen + 1) * sizeof(wchar_t)];
MultiByteToWideChar(CP_ACP, 0, pSrc, -1, pWstr, nLen);
pWstr[nLen] = L'\0';
wcscpy_s(pRst, nLen, pWstr);
delete[]pWstr;
pWstr = nullptr;
}
//宽字节转多字节
void Wide2MultiByte(wchar_t *pSrc, char * pRst)
{
int nLen = WideCharToMultiByte(CP_ACP, 0, pSrc, -1, NULL, 0, NULL, NULL);
char *pStr = new char[(nLen + 1) * sizeof(char)];
WideCharToMultiByte(CP_ACP, 0, pSrc, -1, pStr, nLen, NULL, NULL);
pStr[nLen] = '\0';
strcpy_s(pRst, nLen + 1, pStr);
delete[]pStr;
pStr = nullptr;
}
//UTF8转多字节
void Utf2MultiByte(char *pSrc, char *pRst)
{
//转换为wide
int nLen = MultiByteToWideChar(CP_UTF8, 0, pSrc, -1, NULL, 0);
wchar_t *pWstr = new wchar_t[(nLen + 1) * sizeof(wchar_t)];
MultiByteToWideChar(CP_UTF8, 0, pSrc, -1, pWstr, nLen);
pWstr[nLen] = L'\0';
//wide转多字节
nLen = WideCharToMultiByte(CP_ACP, 0, pWstr, -1, NULL, 0, NULL, NULL);
char *pStr = new char[(nLen + 1) * sizeof(char)];
WideCharToMultiByte(CP_ACP, 0, pWstr, -1, pStr, nLen, NULL, NULL);
pStr[nLen] = '\0';
strcpy_s(pRst, nLen + 1, pStr);
delete[]pWstr;
pWstr = nullptr;
delete[]pStr;
pStr = nullptr;
}
//多字节转UTF8
void MultiByte2Utf8(char * pSrc, char * pRst)
{
int nLen = MultiByteToWideChar(CP_ACP, 0, pSrc, -1, NULL, 0);
wchar_t *pTmp = new wchar_t[(nLen + 1) * sizeof(wchar_t)];
MultiByteToWideChar(CP_ACP, 0, pSrc, -1, pTmp, nLen);
pTmp[nLen] = L'\0';
nLen = WideCharToMultiByte(CP_UTF8, 0, pTmp, -1, NULL, 0, NULL, NULL);
char *pStr = new char[(nLen + 1) * sizeof(char)];
WideCharToMultiByte(CP_UTF8, 0, pTmp, -1, pStr, 0, NULL, NULL);
pStr[nLen] = '\0';
strcpy_s(pRst, nLen + 1, pStr);
delete[]pTmp;
pTmp = nullptr;
delete[]pStr;
pStr = nullptr;
}
C++常见字符转换
最新推荐文章于 2022-01-24 11:20:20 发布