unicode、多字节和utf8互转

#include<windows.h>
#include<stdlib.h>
#include<string>
//多字节转为utf8
int MultiByToUtf8(const char *multi,char *&utf8)
{
	int size = 0;
	size = MultiByteToWideChar(CP_ACP, NULL, multi, -1, NULL, 0);
	wchar_t *buff = NULL;
	buff = (wchar_t *)malloc(sizeof(wchar_t)*(size + 1));
	if (!buff)
	{
		return 0;
	}
	wmemset(buff, 0, size + 1);
	MultiByteToWideChar(CP_ACP, NULL, multi, -1, buff, size);
	int len = 0;
	len = WideCharToMultiByte(CP_UTF8, NULL, buff, size, NULL, 0, NULL, NULL);
	utf8 = NULL;
	utf8 = (char *)malloc(sizeof(char)*(len + 1));
	if (!utf8)
	{
		free(buff);
		return 0;
	}
	memset(utf8, 0, len + 1);
	WideCharToMultiByte(CP_UTF8, NULL, buff, size, utf8, len, NULL, NULL);
	free(buff);
	return len;
}
//utf8转为多字节
int Utf8ToMultiBy(const char *utf8,char *&multi)
{
	int size = 0;
	size = MultiByteToWideChar(CP_UTF8, NULL, utf8, -1, NULL, 0);
	wchar_t *buff = NULL;
	buff = (wchar_t *)malloc(sizeof(wchar_t)*(size + 1));
	if (!buff)
	{
		return 0;
	}
	wmemset(buff, 0, size + 1);
	MultiByteToWideChar(CP_ACP, NULL, utf8, -1, buff, size);
	int len = 0;
	len = WideCharToMultiByte(CP_ACP, NULL, buff, size, NULL, 0, NULL, NULL);
	multi = NULL;
	multi = (char *)malloc(sizeof(char)*(len + 1));
	if (!multi)
	{
		free(buff);
		return 0;
	}
	memset(multi, 0, len + 1);
	WideCharToMultiByte(CP_ACP, NULL, buff, size, multi, len, NULL, NULL);
	free(buff);
	return len;
}
//utf8转unicode
int Utf8ToUnicode(const char *utf8,wchar_t *&unicode)
{
	int len = 0;
	len = MultiByteToWideChar(CP_UTF8, NULL, utf8, -1, NULL, 0);
	unicode = NULL;
	unicode = (wchar_t *)malloc(sizeof(wchar_t)*(len + 1));
	if (!unicode)
	{
		return 0;
	}
	wmemset(unicode, 0, len + 1);
	MultiByteToWideChar(CP_UTF8, NULL, utf8, -1, unicode, len);
	return len;
}
//unicode转utf8
int UnicodeToUtf8(const wchar_t*unicode,char *&utf8)
{
	int len = 0;
	len = WideCharToMultiByte(CP_UTF8, NULL, unicode, -1, NULL, 0, NULL, NULL);
	utf8 = NULL;
	utf8 = (char*)malloc(sizeof(char)*(len + 1));
	memset(utf8, 0, len + 1);
	if (!utf8)
	{
		return 0;
	}
	WideCharToMultiByte(CP_UTF8, NULL, unicode, -1, utf8, len, NULL, NULL);
	return len;
}

//多字节转unicode
int MultiByToUnicode(const char *multi, wchar_t *&unicode)
{
	int len = 0;
	len = MultiByteToWideChar(CP_ACP, NULL, multi, -1, NULL, 0);
	unicode = NULL;
	unicode = (wchar_t *)malloc(sizeof(wchar_t)*(len + 1));
	if (!unicode)
	{
		return 0;
	}
	wmemset(unicode, 0, len + 1);
	MultiByteToWideChar(CP_ACP, NULL, multi, -1, unicode, len);
	return len;
}

//unicode转多字节
int UnicodeToMultiBy(const wchar_t*unicode, char *&multi)
{
	int len = 0;
	len = WideCharToMultiByte(CP_ACP, NULL, unicode, -1, NULL, 0, NULL, NULL);
	multi = NULL;
	multi = (char*)malloc(sizeof(char)*(len + 1));
	memset(multi, 0, len + 1);
	if (!multi)
	{
		return 0;
	}
	WideCharToMultiByte(CP_ACP, NULL, unicode, -1, multi, len, NULL, NULL);
	return len;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值