unicode编码之大端

unicode编码有大端小端,用两个字节表示,用大端编码时,要把低位数据是字符编码的高字节,高字节是字符编码的高字节,文本头为FE FF,

#include 
#include 
#include 

static int isUtf16beCoding(unsigned char *pucBufHead);
static int Utf16beGetCodeFrmBuf(unsigned char *pucBufStart, unsigned char *pucBufEnd, unsigned int *pdwCode);

static T_EncodingOpr g_tUtf16beEncodingOpr = {
	.name          = "utf-16be",
	.iHeadLen	   = 2,
	.isSupport     = isUtf16beCoding,
	.GetCodeFrmBuf = Utf16beGetCodeFrmBuf,
};

static int isUtf16beCoding(unsigned char *pucBufHead)
{
	const char aStrUtf16be[] = {0xFE, 0xFF, 0};//文本头
	
	if (strncmp((const char*)pucBufHead, aStrUtf16be, 2) == 0)//是不是unicode的文本头
	{
		/* UTF-16 big endian */
		return 1;
	}
	else
	{
		return 0;
	}
}

static int Utf16beGetCodeFrmBuf(unsigned char *pucBufStart, unsigned char *pucBufEnd, unsigned int *pdwCode)
{
	if (pucBufStart + 1 < pucBufEnd)
	{
		*pdwCode = (((unsigned int)pucBufStart[0])<<8) + pucBufStart[1];//低字节是字符的高字节,高字节是字符的低字节
		return 2;
	}
	else
	{
		/* 文件结束 */
		return 0;
	}
}

int  Utf16beEncodingInit(void)
{
	AddFontOprForEncoding(&g_tUtf16beEncodingOpr, GetFontOpr("freetype"));
	AddFontOprForEncoding(&g_tUtf16beEncodingOpr, GetFontOpr("ascii"));
	return RegisterEncodingOpr(&g_tUtf16beEncodingOpr);
}


 

你可能感兴趣的:(韦东山老师项目学习笔记)