mxml中文乱码的解决

字符编码格式

判断字符编码格式可参考:https://blog.csdn.net/thedarkfairytale/article/details/73457200

int IsUTF8(const char* str)
{
	unsigned int nBytes = 0;//UFT8可用1-6个字节编码,ASCII用一个字节  
	unsigned char chr = *str;
	int bAllAscii = 1;

	for (unsigned int i = 0; str[i] != '\0'; ++i){
		chr = *(str + i);
		//判断是否ASCII编码,如果不是,说明有可能是UTF8,ASCII用7位编码,最高位标记为0,0xxxxxxx 
		if (nBytes == 0 && (chr & 0x80) != 0){
			bAllAscii = 0;
		}

		if (nBytes == 0) {
			//如果不是ASCII码,应该是多字节符,计算字节数  
			if (chr >= 0x80) {

				if (chr >= 0xFC && chr <= 0xFD){
					nBytes = 6;
				}
				else if (chr >= 0xF8){
					nBytes = 5;
				}
				else if (chr >= 0xF0){
					nBytes = 4;
				}
				else if (chr >= 0xE0){
					nBytes = 3;
				}
				else if (chr >= 0xC0){
					nBytes = 2;
				}
				else{
					return 0;
				}

				nBytes--;
			}
		}
		else{
			//多字节符的非首字节,应为 10xxxxxx 
			if ((chr & 0xC0) != 0x80){
				return 0;
			}
			//减到为零为止
			nBytes--;
		}
	}

	//违返UTF8编码规则 
	if (nBytes != 0)  {
		return 0;
	}

	if (bAllAscii){ //如果全部都是ASCII, 也是UTF8
		return 1;
	}

	return 1;
}

int IsGBK(const char* str)
{
	unsigned int nBytes = 0;//GBK可用1-2个字节编码,中文两个 ,英文一个 
	unsigned char chr = *str;
	int bAllAscii = 1; //如果全部都是ASCII,  

	for (unsigned int i = 0; str[i] != '\0'; ++i){
		chr = *(str + i);
		if ((chr & 0x80) != 0 && nBytes == 0){// 判断是否ASCII编码,如果不是,说明有可能是GBK
			bAllAscii = 0;
		}

		if (nBytes == 0) {
			if (chr >= 0x80) {
				if (chr >= 0x81 && chr <= 0xFE){
					nBytes = +2;
				}
				else{
					return 0;
				}

				nBytes--;
			}
		}
		else{
			if (chr < 0x40 || chr>0xFE){
				return 0;
			}
			nBytes--;
		}//else end
	}

	if (nBytes != 0)  {		//违返规则 
		return 0;
	}

	if (bAllAscii){ //如果全部都是ASCII, 也是GBK
		return 1;
	}

	return 1;
}

XmlAddElemStr的修改部分

int XmlAddElemStr(mxml_node_t *ptRoot, const int8 *szElemName, const int8 *szValue, mxml_node_t **pptElem)
{
	if (!szValue)
	{
		return -1;
	}
	int isGBK = IsGBK(szValue);
	int8* pTmpArr = NULL;
	if (isGBK)
	{
		int tmpLen = strlen(szValue);
		pTmpArr = (int8*)malloc(tmpLen * 3 + 1);
		memset(pTmpArr,0, tmpLen * 3 + 1);
		if (!pTmpArr)
		{
			return -1;
		}
		GB2312ToUTF8(pTmpArr, szValue, tmpLen);
	}
	else
	{
		pTmpArr = szValue;
	}
    mxml_node_t *ptElem = NULL;
    mxml_node_t *ptText = NULL;

    ptElem = mxmlNewElement(ptRoot, szElemName);
    if (NULL == ptElem)
    {
        printf("mxmlNewElement() failed, {%s}\n", szElemName);
        return -1;
    }

	ptText = mxmlNewText(ptElem, 0, pTmpArr);
    if (NULL == ptText)
    {
		if (isGBK &&pTmpArr)
		{
			free(pTmpArr);
			pTmpArr = NULL;
		}
		printf("mxmlNewText() failed, Elem{%s}, value{%s}\n", szElemName, pTmpArr);
        return -1;
    }

    if (pptElem)
    {
        *pptElem = ptElem;
    }
	/*释放内存*/
	if (isGBK &&pTmpArr)
	{
		free(pTmpArr);
		pTmpArr = NULL;
	}
    return 0;
}

编码格式转换

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值