Allow Trichedit to support BIG5 inner code

Source: Internet
Author: User
Tags range

Hello everyone, now software such as: Foxmail software, have display GB inside code and BIG5 function. After a period of exploration, I found the way to achieve this function. Let's introduce it to you now. First of all, I would like to introduce the two types of code: We all know that the English ASCII code is expressed in a byte, but Chinese because the number of words is too much, so can only be expressed in two bytes. In fact, our common text file is stored in the corresponding internal code of each Chinese character, and the Chinese character's inner code is the Chinese character in the logical location of the font. In the Chinese font, this logical location holds the image information of the Chinese character, with these image information, you can display the Chinese character, of course, in the current Windows this display process is automatically completed by the operating system, we do not have to care about it. But for some historical reasons, the Chinese character's internal code scheme has two kinds, our continent uses is the GB code, but Taiwan Province uses is the BIG5 code. GB code file to save the corresponding GB code, BIG5 code file is the corresponding BIG5 code. And both of the internal code corresponding to their own font files, so when we use the GB inner code of the simplified Chinese windows to see BIG5 file, it will produce often said garbled phenomenon.

Because we are to display BIG5 code of Chinese characters, so to introduce the coding scheme of BIG5 code: Each Chinese character consists of two bytes, the first byte range from 0x81-0xfe, the second byte range is 0x40-0x7e,0xa1-0xfe respectively. Well, I know the principle of Chinese character display and BIG5 code coding scheme, you can easily let Trichedit support BIG5 code.

The first step: Find a Code table file for internal code conversion.

I'm using the Ccdos.tab in CCDOS97, the size of this file is 45,840 bytes. If you do not have this document, you can write to me (cnprogram@netease.com) I will send one to you. Of course, you can also according to BIG5 code coding scheme to do one, here is not much to say.

Step two: Convert the Code table file into an array of files. See the following procedure.

Build a console application with BCB. Save it as TABTOHEX.BPR and TABTOHEX.CPP. Then add the following code to the TABTOHEX.cpp:

#pragma hdrstop
#include <condefs.h>
#include"stdio.h"
#include"stdlib.h"
#pragma argsused
int main(int argc, char **argv)
{
   FILE *tab;
   FILE *hex;
   int i=0;
   unsigned int ch;
   tab=fopen("ccdos.tab","rb");//打开CCDOS.TAB文件
   hex=fopen("tab.h","wb");
   fputs("unsigned char tab[45840]={",hex);
   fprintf(hex,"\n");
   for(i=0;i<45840;i++)
   {
     if(i%20==0)
     {
       fprintf(hex,"\n");
     }
     ch=fgetc(tab);
     fprintf(hex,"0x%x\x2c",ch);//把码表文件转换为数组文件
   }
   fputs("0xff};",hex);
   fcloseall();
   return 0;
}

Then compile and execute the program to get the TAB.H file.

Step three: Create a new standard application in BCB3.0. Add a TButton component: Button1 Add a Trichedit component: RichEdit1 adjust their size and position appropriately. Then add the following code to the Unit1.cpp:

#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "tab.h"///Head file TAB.H included
//- --------------------------------------------------------------------------
#pragma package (smart_init)
# pragma resource "*.DFM"
TForm1 *form1;
---------------------------------------------------------------------------
__fastcall Tform1::tform1 ( tcomponent* owner): Tform (owner)
{
}
//---------------------------------------------------------------- -----------
void __fastcall tform1::formcreate (tobject *sender)
{
Richedit1->lines->loadfromfile (" C:\\big5. TXT ");//read a BIG5 code of the file, can set their own situation
}
//---------------------------------------------------------------------- -----
void __fastcall Tform1::button1click (tobject *sender)
{
unsigned long i,address;//convert BIG5 code to GB
UN Signed Char Ch1,ch2;
Char *x;
X=richedit1->lines->gettext ();
I=0;    
while (x[i]!= ')
{
Ch1=x[i];
if (ch1> =0xa1)
{
Ch2=x[i+1];
         if (ch2>=0x40&&ch2<=0x7e)
{
address=ch1*314+ch2*2-33010;
X[i]=tab[address];
X[i+1]=tab[address+1];        
}
if (Ch2>=0xa1&&ch2<=0xfe)
{
address=ch1*314+ch2*2-33078;
X[i]=tab[address];
X[i+1]=tab[address+1];    
}
i+=2;
  
Else
i++;
}
richedit1->lines->text=x;
Richedit1->setfocus ();
Richedit1->selstart = 0;
}

Compile and execute it, and first you will see that the BIG5 code file in RichEdit1 is displayed as garbled. Click Button1, at this time, you will see RichEdit1 in the garbled into a normal Chinese characters. OK, now you make up the software can also support BIG5 code, is not feel more professional. Gb->big5 conversion method is similar, interested friends can give me a letter. Finally, I wish you all a happy dragon. This program is compiled and passed under win97,bcb3.0.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.