字符集ANSI与Unicode的转换及UTF-8编码方案

1.本文提供了一种在Windows下将ANSI、Unicode和UTF-8三者之间相互转换的方法。
2.本文的代码参考来源:https://blog.csdn.net/shufac/article/details/51829267
3.想更深入了解ANSI、Unicode及UFT-8,网上有非常多的资料:
  https://blog.csdn.net/xiongxiao/article/details/3741731
  https://www.zhihu.com/question/20650946

ANSI

简单地来说,就是一种字符代码,通过1个字节来表示一个英文字符,2个字节表示一个中文字,最早的时候计算机ASCII码只能表示256个符号(含控制符号),用ANSI来表示中文字就明显不够用了。于是,人们就发明了Unicode这个字符集,可以囊括目前的所有字符。

Unocide

对于 Unicode,字符集和编码是明确区分的。Unicode/UCS 标准首先是个统一的字符集标准。而 Unicode/UCS 标准同时也定义了几种可选的编码方案,在标准文档中称作「encoding form」,主要包括 UTF-8、UTF-16 和 UTF-32。

UTF-8

UTF-8是针对Unicode的一种可变长度字符编码,是网页比较流行的一种格式:用1个字节表示英文字符,用3个字节表示汉字,准确的说,UTF-8是用二进制编码的前缀。

代码逻辑实现三者之间的相互转换

  1. 定义字符串转换用的类

代码可以放在一个头文件里面实现,因为代码量不是很多,先来定义一个字符转换的类。可以看到,我的目的是将一个字符串作为一个参数,传入转换函数当中,返回一个目标字符串。STL标准库提供了接口,可以使Unicode与ANSI之间相互转换,以及Unicode与UTF-8之间的转换,但是没有UTF-8与ANSI的直接转换,所UTF-8与ANSI之间的相互转换需要通过Unicode来间接转换。

#include <iostream>
#include <string>
#include <stringapiset.h>
//字符串编码格式转换
//STL标注库提供了接口,可以使Unicode <=> ANSI之间相互转换
//Unicode <=> UTF-8,要想UTF-8 <=>ANSI 话,就通过转换Unicode
//来间接转化
//UTF-8:多字节字符串
//Unicode:宽字节字符串

static const int CHAR_MAX_LEN = 512;
class Char_Convert_Method
{
public:
	Char_Convert_Method()
	{
		memset(wstring_, 0, sizeof(wstring_));
		memset(string_, 0, sizeof(string_));
	};
	~Char_Convert_Method()
	{
	};
public:
	//UTF-8 <=> Unicode之间的转化
	wchar_t* AnsiToUnicode(char *str);
	char* UnicodeToAnsi(wchar_t *wstr);

	//Unicode <=> ANSI之间的转化
	char* UnicodeToUtf8(wchar_t *wstr);
	wchar_t* Utf8ToUnicode(char *str);

	//UTF-8 <=> ANSI之间的转化
	char* Utf8ToAnsi(char *str);
	char* AnsiToUtf8(char *str);

public:
	wchar_t wstring_[CHAR_MAX_LEN];
	char string_[CHAR_MAX_LEN];
};
  1. 函数的实现,ANSI<=>Unicode,用到的标准接口及参数说明也在函数前面标注了。
/***********************************************************************************
该函数映射一个字符串到一个宽字符(unicode)的字符串
int MultiByteToWideChar(
	UINT CodePage,
	DWORD dwFlags,
	LPCSTR lpMultiByteStr,
	int cchMultiByte,
	LPWSTR lpWideCharStr,
	int cchWideChar
	);

	1.CodePage: 指定执行转换的多字节字符所使用的字符集  CP_ACP -- ANSI code page
	2.dwFlags: 一组位标记,用以指出是否未转换成预作或宽字符,是否使用象形文字代替控制字符,以及如何处理无效字符
		一般不使用这个标志,取0
	3.lpMultiByteStr: 指向待转换的字符串的缓冲区
	4.cchMultiByte: 指定由参数lpMultiByteStr指向的字符串中字节的个数。可设置为-1,自动判断
	5.lpWideCharStr: 指向接收被转换字符串的缓冲区
	6.cchWideChar:指定由参数lpWideCharStr指向的缓冲区的宽字节数。若为0,函数不会执行转换,而是返回目标缓存lpWideCharStr
		所需的宽字符数

返回值:
	成功且cchWideChar不为0:返回由lpWideCharStr指向的缓冲区中写入的宽字符数
	成功且cchMultiByte为0: 返回待转换字符串的缓冲区所需的宽字符数
	失败返回0
***************************************************************************************/
wchar_t* Char_Convert_Method::AnsiToUnicode(char *str)
{
	int len = ::MultiByteToWideChar(CP_ACP, NULL, str, strlen(str), NULL, 0);
	wchar_t *wstr = new wchar_t[len + 1];
	::MultiByteToWideChar(CP_ACP, NULL, str, strlen(str), wstr, len);
	wstr[len] = '\0';
	
	memcpy(wstring_, wstr, CHAR_MAX_LEN);
	delete wstr;
	return wstring_;
}

/***********************************************************************************
该函数映射一个unicode字符串到一个多字节的字符串
int WideCharToMultiByte(
	UINT CodePage,
	DWORD dwFlags,
	LPCWSTR lpWideCharStr,
	int cchWideChar,
	LPSTR lpMultiByteStr,
	int cchMultiByte,
	LPCSTR lpDefaultChar,
	LPBOOL pfUsedDefaultChar
	);

	这个跟MultiByteToWideChar()函数不多,多了两个参数lpDefaultChar和pfUsedDefaultChar,只有当WideCharToMultiByte函数遇到
	一个宽字节字符,而该字符在uCodePage参数标识的代码页中并没有它的表示法时才会用到,通常取NULL;

返回值:
	成功且cchMultiByte不为0:返回由lpMultiByteStr指向的缓冲区中写入的宽字节数
	成功且cchMultiByte为0: 返回待转换字符串的缓冲区所需的宽字节数
	失败返回0
***************************************************************************************/
char* Char_Convert_Method::UnicodeToAnsi(wchar_t *wstr)
{
	int len = ::WideCharToMultiByte(CP_ACP, NULL, wstr, wcslen(wstr), NULL, 0, NULL, NULL);
	char *str = new char[len + 1];
	::WideCharToMultiByte(CP_ACP, NULL, wstr, wcslen(wstr), str, len, NULL, NULL);
	str[len] = '\0';
	
	memcpy(string_, str, CHAR_MAX_LEN);
	delete str;
	return string_;
}
  1. Unicode <=> UTF-8
char* Char_Convert_Method::UnicodeToUtf8(wchar_t *wstr)
{
	int len = ::WideCharToMultiByte(CP_UTF8, NULL, wstr, wcslen(wstr), NULL, 0, NULL, NULL);
	char *str = new char[len + 1];
	::WideCharToMultiByte(CP_UTF8, NULL, wstr, wcslen(wstr), str, len, NULL, NULL);
	str[len] = '\0';
	
	memcpy(string_, str, CHAR_MAX_LEN);
	delete str;
	return string_;
}

wchar_t* Char_Convert_Method::Utf8ToUnicode(char *str)
{
	int len = ::MultiByteToWideChar(CP_UTF8, NULL, str, strlen(str), NULL, 0);
	wchar_t *wstr = new wchar_t[len + 1];
	::MultiByteToWideChar(CP_UTF8, NULL, str, strlen(str), wstr, len);
	wstr[len] = '\0';
	
	memcpy(wstring_, wstr, CHAR_MAX_LEN);
	delete wstr;
	return wstring_;
}
  1. ANSI <=> UTF-8
char* Char_Convert_Method::Utf8ToAnsi(char *str)
{
	return UnicodeToAnsi(Utf8ToUnicode(str));
}

char* Char_Convert_Method::AnsiToUtf8(char *str)
{
	return UnicodeToUtf8(AnsiToUnicode(str));
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值