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

📄 gbunicode.cpp

📁 ansi和unicode之间的相互转化
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/************************************************************************
*
*  Module name         : GBUnicode.cpp
*
*  Module description  :
*     Convert encoding method between GB2312 and Unicode.
*
*  Project             : Generic
*
*  Target platform     : Win32
*
*  Compiler & Library  : Visual C++ 6.0
*
*  Author              : Richard Shen
*
*  Creation date       : 09 October, 2000
*
************************************************************************/
#include "stdafx.h"
//#include <windows.h>
#include "GBBig5Unicode.h"

// These "extern"s are actually in this file
extern const unsigned char    gb2uTable[];
extern const long             gb2uCount;
extern const unsigned char    u2gbTable[];
extern const long             u2gbCount;

/************************************************************************
*  Function name   : GBCodeToUnicode
*  Description     : Map GB2312 code to Unicode.
*                  :
*  Parameters      : gbCode   - The GB code.
*  Returns         : The mapped Unicode.  NULL if error.
*  Author          : Richard Shen
* ----------------------------------------------------------------------
*  Date     By       Description
* ----------------------------------------------------------------------
*  09Oct00  RCS      Created.
************************************************************************/
const unsigned char *GBCodeToUnicode(unsigned char *gbCode)
{
   const unsigned char  *mapped = 0;
   unsigned int         i;

   if ((*(gbCode + 1) >= 0xa1) && (*(gbCode + 1) <= 0xfe))
   {
      if ((*gbCode >= 0xa1) && (*gbCode <= 0xa9))
      {
         i      = ((*gbCode - 0xa1) * 94 + (*(gbCode + 1) - 0xa1)) * 2;
         mapped = &gb2uTable[i];
      } /* end of if */
      else
      if ((*gbCode >= 0xb0) && (*gbCode <= 0xf7))
      {
         i      = ((*gbCode - 0xb0 + 9) * 94 + (*(gbCode + 1) - 0xa1)) * 2;
         mapped = &gb2uTable[i];
      } /* end of if */
    } /* end of if */

   return mapped;
} // GBCodeToUnicode()

/************************************************************************
*  Function name   : UnicodeToGBCode
*  Description     : Map Unicode to GB2312 code.
*                  :
*  Parameters      : unicode  - The Unicode.
*  Returns         : The mapped GBCode.  NULL if error.
*  Author          : Richard Shen
* ----------------------------------------------------------------------
*  Date     By       Description
* ----------------------------------------------------------------------
*  09Oct00  RCS      Created.
************************************************************************/
const unsigned char *UnicodeToGBCode(unsigned char *unicode)
{
   unsigned int   i;
	if(!*(unicode + 1))return 0;
   i = (*unicode  + (*(unicode + 1)<< 8)) * 2;

   return &u2gbTable[i];
   /*if(IsGBCode((unsigned char *)&u2gbTable[i]))
	   return &u2gbTable[i];
   else
	   return 0;
	*/
} // UnicodeToGBCode()

/************************************************************************
*  Function name   : IsGBCode
*  Description     : Check whether it is a valid GB2312 code.
*                  :
*  Parameters      : gbCode   - The code (2 bytes).
*  Returns         : TRUE     - Is GB code.
*                  : FALSE    - Non GB code.
*  Author          : Richard Shen
* ----------------------------------------------------------------------
*  Date     By       Description
* ----------------------------------------------------------------------
*  20Oct00  RCS      Created.
************************************************************************/
BOOL IsGBCode(unsigned char *gbCode)
{
   BOOL  answer = FALSE;

   if ((*(gbCode + 1) >= 0xa1) && (*(gbCode + 1) <= 0xfe))
   {
      if ((*gbCode >= 0xa1) && (*gbCode <= 0xa9))
         answer = TRUE;
      else
      if ((*gbCode >= 0xb0) && (*gbCode <= 0xf7))
         answer = TRUE;
    } /* end of if */

   return answer;
} // IsGBCode()

int StrGBCodeToUnicode(char *gbCode,TCHAR *unicode)
{
	unsigned char* pGbCode=(unsigned char*)gbCode;
	unsigned char* pUniCode=(unsigned char*)unicode;
	int i,j;
	int len1=strlen((char*)pGbCode);
	for(i=0,j=0;i<len1;i++)
	{
		unsigned char *pReturn=(unsigned char *)GBCodeToUnicode(pGbCode+i);
		if(pReturn)
		{
			//gbx[j++]=*pTChar;
			//i++;
			pUniCode[j++]=*(pReturn+1);
			pUniCode[j++]=*pReturn;
			i++;
		}
		else
		{
			pUniCode[j++]=pGbCode[i];
			pUniCode[j++]=0;
			//gbx[j++]=*(hao+i);
		}
	}
	pUniCode[j]=0;
	pUniCode[j+1]=0;
	return j/2;
}

int StrUnicodeToGBCode(TCHAR *unicode,char *gbCode)
{
	unsigned char* pGbCode=(unsigned char*)gbCode;
	unsigned char* pUniCode=(unsigned char*)unicode;
	int i,j;
	int len1=wcslen((unsigned short*)pUniCode);
	int len2=len1*2;
	for(i=0,j=0;i<len2;i+=2)
	{
		unsigned char *pReturn=(unsigned char *)UnicodeToGBCode(pUniCode+i);
		if(pReturn)
		{
			pGbCode[j++]=*pReturn;
			pGbCode[j++]=*(pReturn+1);			
		}
		else
		{
			pGbCode[j++]=*(pUniCode+i);		
		}
	}
	pGbCode[j]=0;	
	return j;
}
/************************************************************************
*                       GB2312 to Unicode mapping                       *
************************************************************************/
const unsigned char  gb2uTable[] =
{
   0x30,0x00, 0x30,0x01, 0x30,0x02, 0x30,0xfb, 0x02,0xc9, 0x02,0xc7, 0x00,0xa8,
   0x30,0x03, 0x30,0x05, 0x20,0x15, 0xff,0x5e, 0x22,0x25, 0x20,0x26, 0x20,0x18,
   0x20,0x19, 0x20,0x1c, 0x20,0x1d, 0x30,0x14, 0x30,0x15, 0x30,0x08, 0x30,0x09,
   0x30,0x0a, 0x30,0x0b, 0x30,0x0c, 0x30,0x0d, 0x30,0x0e, 0x30,0x0f, 0x30,0x16,
   0x30,0x17, 0x30,0x10, 0x30,0x11, 0x00,0xb1, 0x00,0xd7, 0x00,0xf7, 0x22,0x36,
   0x22,0x27, 0x22,0x28, 0x22,0x11, 0x22,0x0f, 0x22,0x2a, 0x22,0x29, 0x22,0x08,
   0x22,0x37, 0x22,0x1a, 0x22,0xa5, 0x22,0x25, 0x22,0x20, 0x23,0x12, 0x22,0x99,
   0x22,0x2b, 0x22,0x2e, 0x22,0x61, 0x22,0x4c, 0x22,0x48, 0x22,0x3d, 0x22,0x1d,
   0x22,0x60, 0x22,0x6e, 0x22,0x6f, 0x22,0x64, 0x22,0x65, 0x22,0x1e, 0x22,0x35,

⌨️ 快捷键说明

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