#include <stdio.h> #include <stdlib.h> #include <windows.h> //GB2312将代码表分为94个区,每个区94位 #define GB2312_MATRIX (94) //第二字节开始编码 #define DELTA (0xA0) //第一字节以0xB0开始 #define FONT_ROW_BEGIN (0xB0) //第一个字节以0xF7结束 #define FONT_ROW_END (0xF7) //第二个字节以0xA1开始 #define FONT_COL_BEGIN (0xA1) //第二个字节以0xFE结束 #define FONT_COL_END (0xFE) //16-87区为汉字区 #define FONT_TOTAL (((87-16)+1) * GB2312_MATRIX) void UnicodeToGB2312(unsigned char* pOut, unsigned short uData); void Gb2312ToUnicode(unsigned short* pOut,unsigned char* gbBurrer); void construct_unicode_table(); int main(int argc, char* argv[]) { construct_unicode_table(); return 0; } void construct_unicode_table() { int i,j; unsigned char chr[2]; unsigned short uni; unsigned short data[FONT_TOTAL] = {0}; int index = 0; unsigned short buf; //生成unicode码表 for (i=FONT_ROW_BEGIN;i<=FONT_ROW_END;i++) { for (j=FONT_COL_BEGIN;j<=FONT_COL_END;j++) { chr[0] = i; chr[1] = j; Gb2312ToUnicode(&uni,chr); data[index] = uni; index++; } } //排序 for (i=0;i<index-1;i++) { for (j=i+1;j<index;j++) { if (data[i]>data[j]) { buf = data[i]; data[i] = data[j]; data[j] = buf; } } } //输出 printf("const unsinged short uni_table[]= {/n"); for (i=0;i<index;i++) { uni = data[i]; UnicodeToGB2312(chr,uni); char sChar[3]; strncpy(sChar,(const char*)chr,2); sChar[2] = '/0'; printf(" 0x%.4X%s /* GB2312 Code: 0x%.2X%.2X ==>%s Row:%.2d Col:%.2d *//n", uni, i==index-1?" ":",", chr[0], chr[1], sChar, chr[0] - DELTA, chr[1] - DELTA ); } printf("};/n"); return; } void UnicodeToGB2312(unsigned char* pOut,unsigned short uData) { WideCharToMultiByte(CP_ACP,NULL,&uData,1,(LPSTR)pOut,sizeof(unsigned short),NULL,NULL); return; } void Gb2312ToUnicode(unsigned short* pOut,unsigned char *gbBuffer) { MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,(LPSTR)gbBuffer,2,pOut,1); return; }