Unicode to Gbk,gnk to Unicode, to solve the problem of fatfs in the use of ROM in Chinese code table

Source: Internet
Author: User

Source: Unicode to Gbk,gnk to Unicode, to solve the problem of FATFS the ROM occupied by the Chinese Code table

Before the use of the 512KB ROM STM32, but recently used only 128KB, want to use FATFS display support long filenames, found that add cc936.c after the ROM is not enough, decided to store this bidirectional code table in the external memory, flash or SD card line, Only can read on the line;

The encoded conversion function in the changed CC936.C

WCHAR Ff_convert (/*converted code, 0 means conversion error*/WCHAR SRC,/*Character code to be converted*/UINT dir/*0:unicode to OEMCP, 1:OEMCP to Unicode*/) {WCHAR C; if(Src <0x80) {        /*ASCII*/C=src; }     Else     {        if(dir) {/*OEMCP to Unicode*/C=Gbktounicode (SRC); }         Else         { /*Unicode to OEMCP*/C=UNICODETOGBK (SRC); }    }    returnC;} 


By deleting those two yards, the code instantly reduced by hundreds of KB.

//I'm using w25x16.//Storage Location Description//0x00 ~ 0xbc00 storage Utog.sys 42kb//0xc000 ~ 0x17c00 storage Gtou.sys 47kb//0x18000 ~ 0xd3800 storage 12x12.ttf 750kb//0xd3c00 ~ 0x18f400 storage 16x16.ttf 750kb//base addresses for each file#defineCode_utog_base (0x00)//Unicode to GBK code table#defineCode_gtou_base (0xc000)//GBK to Unicode code table#defineFont_12x12_base (0x18000)//12X12GBK Font#defineFont_16x16_base (0XD3C00)//16X16GBK Font

Let's see how these two functions are implemented

/************************************************************************************************************  * Function: U16 Gbktounicode (U16 Gbkcode) * Function: Convert GBK encoding to Unicode encoding * parameter: GBK * return: Unicode * dependency: Underlying read-write function * Author: Chen Peng * Time: 20120602 * Last modified: 20120602 * Description: Requires the Code table in Flash support GBK code range, high 8-bit: 0X81~0XFE; low 8-bit: 0x40~0xfe******************* ******************************************************************************************************/U16 Gbktounicode (U16 Gbkcode) {U16 Unicode; U8 buff[2]; U16*p;  U8 Ch,cl; CH= Gbkcode >>8; CL= Gbkcode &0x00ff;//Calculate offsetif(CL <0x7f) Unicode= (ch-0x81)* the+cl-0x40;if(Cl >0x80) Unicode= (ch-0x81)* the+cl-0x41; Unicode*=2; W25x16_read (Buff,code_gtou_base+ Unicode,2) ;//Reading Code tableP= (U16 *) buff;return*p;}/************************************************************************************************************  * Function: U16 UNICODETOGBK (U16 Unicode) * Function: Convert Unicode encoding to GBK encoding * parameter: Unicode * return: GBK * Dependency: Underlying read-write function * Author: Chen Peng * Time: 20120602 * Last modified: 20120602 * Description: Requires the Code table in Flash support GBK code range, high 8-bit: 0X81~0XFE; low 8-bit: 0x40~0xfe******************* ******************************************************************************************************/U16 UNICODETOGBK (U16 Unicode)//Two-point lookup algorithm{u32 offset; U8 temp[2]; U16 Res;if(unicode<=0x9fa5) offset=unicode-0x4e00;Else if(unicode>0x9fa5)//It's punctuation .{if(unicode<0xff01|| Unicode>0xff61)return 0;//no corresponding codeoffset=unicode-0xff01+0x9fa6-0x4e00; } w25x16_read (Temp,offset*2+code_utog_base,2);//Get GBK Coderes=temp[0]; Res<<=8; Res+=temp[1]; returnRes;//returns the found encoding}

Just change the w25x16_read () this underlying IO interface based on the memory you're using.

Unicode to Gbk,gnk to Unicode, solve Fatfs Chinese Code table ROM problem (turn)

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.