Unicode to GBK and gnk to Unicode, solving the problem of Rom occupation by fatfs Chinese code table

Source: Internet
Author: User

I have been using the KB Rom stm32, but I only recently used kb. I want to use fatfs to display the support for long file names. I found that the Rom is not enough after cc936.c is added, it is decided to store this two-way code table in external memory, flash or SD card, only can read;

Encoding conversion function in cc936.c after modification
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 );
}
}

Return C;
}

After deleting the two code tables, the Code instantly decreases by several hundred kb.

// I am using w25x16
// Storage location description
// 0x00 ~ 0xbc00
Storage
Utog. sys 42kb
// 0xc000 ~
0x17c00
Store gtou. sys
47kb
// 0x18000 ~
0xd3800 Storage
12x12. TTF 750kb
// 0xd3c00 ~
0x18f400 Storage
16x16. TTF 750kb

// Base address of each file
# Define code_utog_base (0x00)
// Unicode to GBK code table
# Define code_gtou_base (0xc000)
// GBK to Unicode code table
# Define font_12x12_base (0x18000)
// 12x12gbk font
# Define font_16x16_base (0xd3c00)
// 16x16gbk font

The following describes the implementation methods of these two functions.

/*************************************** **************************************** **************************************** **
* Function:
2010gbktounicode (2010gbkcode)
* Function:
Convert GBK encoding to unicode encoding
* Parameters:
GBK
* Return value:
Unicode
* Dependency:
Underlying read/write Functions
* Author:
Chen Peng
* Time:
20120602
* Last modification time: 20120602
* Note: Support for code tables in Flash is required.
GBK code range, 8-bit high: 0x81 ~ 0xfe; low 8 bits: 0x40 ~ 0xfe
**************************************** **************************************** **************************************** */
2010gbktounicode (2010gbkcode)
{
2010unicode;
U8 buff [2];
2010* P;
U8 CH, Cl;

Ch = gbkcode> 8;
CL = gbkcode & 0x00ff;

// Calculate the offset
If (CL <0x7f)
Unicode = (ch-0x81) * 190 + cl-0x40;
If (Cl> 0x80)
Unicode = (ch-0x81) * 190 + cl-0x41;
Unicode * = 2;

W25x16_read (buff, code_gtou_base + Unicode, 2); // read the code table

P = (2010*) Buff;
Return * P;
}

/*************************************** **************************************** **************************************** **
* Function:
Unicodetogbk (UNICODE)
* Function:
Convert Unicode to GBK Encoding
* Parameters:
Unicode
* Return value:
GBK
* Dependency:
Underlying read/write Functions
* Author:
Chen Peng
* Time:
20120602
* Last modification time: 20120602
* Note: Support for code tables in Flash is required.
GBK code range, 8-bit high: 0x81 ~ 0xfe; low 8 bits: 0x40 ~ 0xfe
**************************************** **************************************** **************************************** */
Unicodetogbk (UNICODE) // use a Binary Search Algorithm
{
U32 offset;
U8 temp [2];
2010res;
If (UNICODE <= 0x9fa5) offset = unicode-0X4E00;
Else if (UNICODE> 0x9fa5) // is a punctuation mark
{
If (UNICODE <0xff01 | Unicode> 0xff61) return 0; // no corresponding Encoding
Offset = unicode-0XFF01 + 0x9fa6-0x4e00;
}
W25x16_read (temp, offset * 2 + code_utog_base, 2); // obtain the GBK code
Res = temp [0];
Res <= 8;
Res + = temp [1];
 
Return res; // return the encoding found.
}

You only need to change the underlying IO Interface w25x16_read () based on your memory.

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.