⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 codingconv.cpp

📁 SQlite 的使用 读库 写库
💻 CPP
字号:
// CodingConv.cpp: implementation of the CCodingConv class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "CodingConv.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

__inline void Gb2312_2_Unicode(USHORT* pusDst, const char* pszSrc)
{
	::MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, pszSrc, 2, pusDst, 1);
}

__inline void Unicode_2_UTF8(char* pszDst, USHORT* pusSrc)
{
	char* pChar = (char *)pusSrc;
	
	pszDst[0] = (0xE0 | ((pChar[1] & 0xF0) >> 4));
	pszDst[1] = (0x80 | ((pChar[1] & 0x0F) << 2)) + ((pChar[0] & 0xC0) >> 6);
	pszDst[2] = (0x80 | ( pChar[0] & 0x3F));
}

__inline void UTF8_2_Unicode(USHORT* pusDst, const char* pszSrc)
{
	char* pChar = (char *)pusDst;
	
	pChar[1] = ((pszSrc[0] & 0x0F) << 4) + ((pszSrc[1] >> 2) & 0x0F);
	pChar[0] = ((pszSrc[1] & 0x03) << 6) + (pszSrc[2] & 0x3F);
}

__inline void Unicode_2_GB2312(char* pDst, const USHORT usData)
{
	WideCharToMultiByte(CP_ACP, NULL, &usData, 1, pDst, sizeof(unsigned short), NULL, NULL);
}

int CCodingConv::GB2312_2_UTF8(char* pszDstBuf, int nDstLen, const char* pszSrc, int nSrcLen)
{
	if (pszDstBuf == NULL || pszSrc == NULL)
	{
		return 0;
	}

	if (0 == nSrcLen)
	{
		nSrcLen = strlen(pszSrc);
	}

	int j = 0;
	for (int i=0; i<nSrcLen;)
	{
		if (j >= nDstLen - 1)
			break;

		if (pszSrc[i] >= 0)
		{
			pszDstBuf[j++] = pszSrc[i++];
		}
		else
		{
			USHORT usData = 0;
			Gb2312_2_Unicode(&usData, pszSrc + i);

			char sztmp[4] = "";
			Unicode_2_UTF8(sztmp, &usData);
			
			pszDstBuf[j+0] = sztmp[0];
			pszDstBuf[j+1] = sztmp[1];
			pszDstBuf[j+2] = sztmp[2];
			
			i += 2;
			j += 3;
		}
	}

	pszDstBuf[j] = '\0';

	return j;
}

int CCodingConv::UTF8_2_GB2312(char* pszDstBuf, int nDstLen, const char* pszSrc, int nSrcLen)
{
	if (pszDstBuf == NULL || pszSrc == NULL)
	{
		return 0;
	}

	if (0 == nSrcLen)
	{
		nSrcLen = strlen(pszSrc);
	}

	int j = 0;
	for (int i=0; i<nSrcLen;)
	{
		if (j >= nDstLen - 1)
			break;

		if (pszSrc[i] >= 0)
		{
			pszDstBuf[j++] = pszSrc[i++];
		}
		else
		{
			USHORT usData = 0;
			UTF8_2_Unicode(&usData, pszSrc + i);
			
			char szTmp[4] = "";
			Unicode_2_GB2312(szTmp, usData);
			
			pszDstBuf[j+0] = szTmp[0];
			pszDstBuf[j+1] = szTmp[1];
			
			i += 3;
			j += 2;
		}
	}
	
	pszDstBuf[j] = '\0';
	
	return j;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -