c/c++字符之间进行转换

版权声明:就算是个菜鸟,我也有版权 https://blog.csdn.net/qq_33248019/article/details/88370200
#include <windows.h>
#include <sstream>
#include <string>
//Unicode to GBK(UTF-8 TO Char)
void UnicodeToChar(char *outstr, const char *instr);

//GBK to Unicode(Char to UTF-8)
void CharToUnicode(char *outstr, const char *instr);

//MultiBye to WideChar
void CharToWchar(const char *c, wchar_t *wc);

//WideChar to MultyByte
void WcharToChar(const wchar_t *wc, char *c);

void CharToWchar(const char *c, wchar_t *wc)
{
	int length;
	length = MultiByteToWideChar(CP_ACP, NULL, c, strlen(c) + 1, NULL, NULL);
	MultiByteToWideChar(CP_ACP, NULL, c, strlen(c) + 1, wc, length);
}
void WcharToChar(const wchar_t *wc, char *c)
{
	int length;
	length = WideCharToMultiByte(CP_ACP, NULL, wc, -1, NULL, NULL, NULL, NULL);
	WideCharToMultiByte(CP_ACP, NULL, wc, -1, c, length, nullptr, nullptr);
}

void UnicodeToChar(char * outstr, const char * instr)
{
	int len = MultiByteToWideChar(CP_UTF8, 0, instr, -1, NULL, 0);
	wchar_t *wstr = new wchar_t[len + 1];
	memset(wstr, 0, len + 1);
	MultiByteToWideChar(CP_UTF8, 0, instr, -1, wstr, len);
	len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
	WideCharToMultiByte(CP_ACP, 0, wstr, -1, outstr, len, NULL, NULL);
	if (wstr)
		delete[] wstr;
}

void CharToUnicode(char * outstr, const char * instr)
{
	int len = MultiByteToWideChar(CP_ACP, 0, instr, -1, NULL, 0);
	wchar_t* wstr = new wchar_t[len + 1];
	memset(wstr, 0, len + 1);
	MultiByteToWideChar(CP_ACP, 0, instr, -1, wstr, len);
	len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
	char* str = new char[len + 1];
	memset(str, 0, len + 1);
	WideCharToMultiByte(CP_UTF8, 0, wstr, -1, outstr, len, NULL, NULL);
	if (wstr) 
		delete[] wstr;
}

猜你喜欢

转载自blog.csdn.net/qq_33248019/article/details/88370200