📄 ucs2gbk.cpp
字号:
// UCS2GBK.cpp: implementation of the UCS2GBK class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "TransTool.h"
#include "UCS2GBK.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
UCS2GBK::UCS2GBK()
{
}
UCS2GBK::~UCS2GBK()
{
}
// ucs2GB.c
// unic <==>gbk
/*************************
*
*
***************************/
extern const unsigned short unic2gbk_table[];
extern const unsigned short gbk2unic_table[];
int UCS2GBK::Unicode2GBK(short *unicode,unsigned short slen,unsigned char *GBK,unsigned short dlen)// return strlen
{
unsigned short *pu;
unsigned char *pc;
unsigned short cc;
unsigned short sl=0,dl=0;
int count = 0;
pu=(unsigned short *)unicode;
pc=GBK;
while(*pu)
{
if(*pu<0x80)
{
if((dl+1)<dlen)
{
*pc++=(unsigned char)*pu++;
sl++; dl++;
}
else
break;
}
else if(*pu>=0x4e00)
{
cc=unic2gbk_table[*pu-0x4e00];
pu++;
sl++;
if((dl+2)<dlen)
{
*pc++=cc>>8;
*pc++=cc&0xff;
dl+=2;
}
else
break;
}
else
{
pu++; // lost it , 128--0x4e00 , do not valid at GBK , not complete ,need reference the GBK table
sl++;
}
}
*pc=0;
return pc-GBK;
}
#define LINE_NUM 8
int UCS2GBK::GBK2Unicode(unsigned char *GBK,unsigned short slen,unsigned char *unicode,unsigned short dlen)
{
unsigned short *pu;
unsigned char *pc;
unsigned short cc[LINE_NUM];
unsigned short sl=0,dl=0;
char str[20];
int count = 0, cnt=0;
int offset = 10;
//pu=unicode;
pc=GBK;
memset(str,0,20);
while(*pc)
{
if(*pc<0x80)
{
if((dl+1)<dlen)
{
//*pu++=*pc++;
cc[count] = *pc++;
sl++; dl++;
sprintf(str,"0x%02X,0x%02X,",(unsigned char)((cc[count]&0xff00)>>8),(unsigned char)(cc[count]&0xff));
cnt++;
memcpy((char*)(unicode+offset),str,strlen(str));
offset += strlen(str);
memset(str,0,20);
count ++;
if(count == LINE_NUM)
{
memcpy((char*)(unicode+offset),"\r\n",2);
offset += 2;
count = 0;
}
}
else
break;
}
else
if(*pc>=0x81 && *(pc+1)>=0x40)
{
if((dl+1)<dlen)
{
//*pu++=gbk2unic_table[((*pc-0x80)<<8)+*(pc+1)];
cc[count]=gbk2unic_table[((*pc-0x80)<<8)+*(pc+1)];
pc+=2;
sl+=2; dl++;
sprintf(str,"0x%02X,0x%02X,",(unsigned char)((cc[count]&0xff00)>>8),(unsigned char)(cc[count]&0xff));
cnt++;
memcpy((char*)(unicode+offset),str,strlen(str));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -