C++实现字符格式相互转换的示例代码

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C++实现字符格式相互转换的示例代码

欧特克_Glodon   2022-11-16 我要评论

一、UTF8转std:string

std::string UTF8_To_string(const std::string& str)
{
	int nwLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
	wchar_t* pwBuf = new wchar_t[nwLen + 1];    //一定要加1,不然会出现尾巴 
	memset(pwBuf, 0, nwLen * 2 + 2);
	MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), pwBuf, nwLen);
	int nLen = WideCharToMultiByte(CP_ACP, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
	char* pBuf = new char[nLen + 1];
	memset(pBuf, 0, nLen + 1);
	WideCharToMultiByte(CP_ACP, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);

	std::string strRet = pBuf;

	delete[]pBuf;
	delete[]pwBuf;
	pBuf = NULL;
	pwBuf = NULL;

	return strRet;
}

二、string_To_UTF8

std::string string_To_UTF8(const std::string& str)
{
	int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
	wchar_t* pwBuf = new wchar_t[nwLen + 1];    //一定要加1,不然会出现尾巴 
	ZeroMemory(pwBuf, nwLen * 2 + 2);
	::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen);
	int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
	char* pBuf = new char[nLen + 1];
	ZeroMemory(pBuf, nLen + 1);
	::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);

	std::string strRet(pBuf);

	delete[]pwBuf;
	delete[]pBuf;
	pwBuf = NULL;
	pBuf = NULL;

	return strRet;
}

三、wstring转string

std::string wstring2string(std::wstring wstr)
{
	string result;
	int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);
	if (len <= 0)return result;
	char* buffer = new char[len + 1];
	if (buffer == NULL)return result;
	WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL);
	buffer[len] = '\0';
	result.append(buffer);
	delete[] buffer;
	return result;
}

四、string转wstring

std::wstring  string2wstring(std::string str)
{
	wstring result;
	int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), NULL, 0);
	if (len < 0)return result;
	wchar_t* buffer = new wchar_t[len + 1];
	if (buffer == NULL)return result;
	MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), buffer, len);
	buffer[len] = '\0';
	result.append(buffer);
	delete[] buffer;
	return result;
}

五、unicode属性和多字节属性转换(char转wchar_t)

// WideChar.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include <windows.h>
#include <iostream>
#include <atlbase.h>
//#include <comutil.h>
#include <atlstr.h>
using namespace std;

//方法1:MultiByteToWideChar, WideCharToMultiByte
void Way_1()
{
	//char转wchar_t
	char *p1 = "abc";
	wchar_t p2[20] = L"EFG";
	MultiByteToWideChar(CP_ACP, 0, p1, strlen(p1) + 1, p2, sizeof(p2));
	printf("%S\n", p2);

	//wchar_t转char
	wchar_t *p3= L"EFG";
	char p4[20];
	WideCharToMultiByte(CP_ACP, 0, p3, -1, p4, sizeof(p4), NULL, NULL);
	printf("%s\n", p4);

}

//方法2:A2W, W2A, T2A, A2T
void Way_2()
{
	//需要添加头文件 <atlbase.h>
	char * p1 = "abc";
	wchar_t *p2 = L"def";
	TCHAR *P3 = _T("CC");

	USES_CONVERSION;
	wchar_t *p5 = A2W(p1);
	char * p4 = T2A(P3);
}

方法3:
//void Way_3()
//{//需要添加头文件<comutil.h>,一般在MFC工程下使用
//	CString str = "abc";//只能存一种
//	_bstr_t bstr = "abc";//可以用非unicode
//	bstr += L"efg";//可以用unicode
//	char *p = bstr;
//	wchar_t *p2 = bstr;
//}

int main()
{
	Way_1();
	//Way_2();

	return 0;
}

六、Unicode ansi utf8转换

int Unicode2UTF8(const wchar_t* pUnicode, char* pUTF8Buffer, int nBufferSize)
{
	if( (nBufferSize == 0) && (pUTF8Buffer != NULL) )
		return 0;

	int result = WideCharToMultiByte(CP_UTF8, NULL, pUnicode, -1, pUTF8Buffer, nBufferSize, NULL, NULL);
	if ((result > 0) && (pUTF8Buffer != NULL))
		pUTF8Buffer[result-1] = 0;

	return result;
}

int UTF82Unicode(const char* pUTF8, wchar_t* pUnicodeBuffer, int nBufferSize)
{
	if( (nBufferSize == 0) && (pUnicodeBuffer != NULL) )
		return 0;

	int result = MultiByteToWideChar(CP_UTF8, NULL, pUTF8, -1, pUnicodeBuffer, nBufferSize);
	if ((result > 0) && (pUnicodeBuffer != NULL))
		pUnicodeBuffer[result-1] = 0;

	return result;
}

int Unicode2Ansi(const wchar_t* pUnicode, char* pAnsiBuffer, int nBufferSize)
{
	if( (nBufferSize == 0) && (pAnsiBuffer != NULL) )
		return 0;

	int result = ::WideCharToMultiByte(CP_ACP, 0, pUnicode, -1, pAnsiBuffer, nBufferSize, NULL, NULL);
	if ((result > 0) && (pAnsiBuffer != NULL))
		pAnsiBuffer[result-1] = 0;

	return result;
}

int Ansi2Unicode(const char* pAnsi, wchar_t* pUnicodeBuffer, int nBufferSize)
{
	if( (nBufferSize == 0) && (pUnicodeBuffer != NULL) )
		return 0;

	int result = ::MultiByteToWideChar(CP_ACP, 0, pAnsi, -1, pUnicodeBuffer, nBufferSize);
	if ((result > 0) && (pUnicodeBuffer != NULL))
		pUnicodeBuffer[result-1] = 0;

	return result;
}

简单实用

CString strBuf;
int nSize = Unicode2UTF8(strBuf, NULL, 0);
char* szBuf = new char[nSize];
Unicode2UTF8(strBuf, szBuf, nSize);

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们