Fatfs external Unicode GBK bidirectional conversion code table

Source: Internet
Author: User
Tags aliyun

Put the utog and gtou two-way code table in the memory card to implement long file names, because the fatfs long file names require Unicode support,

First. sys, gtou. sys files are placed in the root directory of the SD card. Note that they must be in the root directory and are short file names. Because long file names require Unicode support, fatfs does not support long file names at this time, however, after the Unicode code table is initialized, long file names are supported.

Two tables: http://download.csdn.net/detail/cp1300/5526739

Attach C code

/*************************************** **************************************** * ****************************** File Name: unicode_gbk.c * function: Chinese character encoding conversion * Author: cp1300@139.com * Creation Time: * last modification time: * Details: you need to support ************************************* **************************************** * ******************************/# include "system. H "# include" unicode_gbk.h "# define gbk_unicode_is_sdcard1 // GBK, Unicode encoding table in SD card or other storage /////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////// code table in the SD card # If gbk_unicode_is_sdcard # include "ff. H "# define gtou" 0:/gtou. sys "// GBK to ucicode encoding table location # define utog" 0:/utog. sys "// ucicode to GBK encoding table location static fil gtou_file; // gtou file workspace static fil utog_file; // utog file workspace /********************************** ************ **************************************** * *********************************** Function: u8 gbk_unicode_init (void) * function: Initialize GBK, Unicode encoding table * parameter: none * returned: 0: initialization successful; others: initialization failed * dependency: underlying read/write function * Author: cp1300@139.com * Time: 2013-04-18 * last modification time: 2013-04-18 * description: no *************************************** **************************************** **************************************** **/u8 gbk_unicode_init (void) {fresult status; status = F_open (& utog_file, utog, fa_open_existing | fa_read); // open the unicodetogbk code table in read-only mode. If (status! = Fr_ OK) // opening failed {uart_printf ("Open % s error (% d )! \ R \ n ", utog, status); return 1;} status = f_open (& gtou_file, gtou, fa_open_existing | fa_read); // open the gbktounicode code table in read-only mode, if (status! = Fr_ OK) // opening failed {uart_printf ("Open % s error (% d )! \ R \ n ", gtou, status); return 1 ;}return 0 ;} /*************************************** **************************************** **************************************** * ** function: 2. onegbktounicode (gbkcode) * function: Convert GBK encoding to Unicode * parameter: GBK * return: Unicode * dependency: underlying read/write function * Author: cp1300@139.com * Time: 20120602 * last modification time: 20120602 * Note: the code table in Flash needs to support the GBK code range. The height is 8 bits: 0x81 ~ 0xfe; low 8 bits: 0x40 ~ 0xfe *************************************** **************************************** **************************************** **/2010onegbktounicode (2010gbkcode) {u8 CH, Cl; uint bw; b2data; CH = gbkcode> 8; CL = gbkcode & 0x00ff; ch-= 0x81; CL-= 0x40; f_lseek (& gtou_file, (Ch * 0xbf + Cl) * 2); // adjust the file pointer to the offset position if (f_read (& gtou_file, (u8 *) & Data, 2, & BW )! = Fr_ OK) // read 2 bytes {return 0x1fff;} return (CH <= 0x7d & CL <= 0xbe )? Data: 0x1fff;/* Ch = gbkcode> 8; CL = gbkcode & 0x00ff; ch-= 0x81; CL-= 0x40; return (CH <= 0x7d & CL <= 0xbe )? Wunicodes [CH * 0xbf + Cl]: 0x1fff; */}/************************************ **************************************** **************************************** * ***** function: feature: Convert Unicode to GBK encoding * parameter: Unicode * return: GBK * dependency: underlying read/write function * Author: cp1300@139.com * Time: 20120602 * last modification time: 20120602 * Note: the code table in Flash needs to support the GBK code range. The height is 8 bits: 0x81 ~ 0xfe; low 8 bits: 0x40 ~ 0xfe *************************************** **************************************** **************************************** **/service.eu-central-1.maxcompute.aliyun-inc.com/api) // use the binary search algorithm {u32 offset; 2010temp; uint bw; u8 buff [2]; If (UNICODE <= 0x9fa5) {If (UNICODE> = 0x4e00) offset = unicode-0X4E00; // 0x1b87 // 0x4e00, Chinese Character offset starting point elsereturn 0x2020; // the characters that cannot be displayed fill with two spaces, otherwise garbled code} else if (UNICODE> 0x9fa5) // is a punctuation {If (UNICODE <0xff01 | Unicode> 0xff61) Re Turn 0x2020; // There is no corresponding encoding // the characters that cannot be displayed fill the two spaces, otherwise garbled offset = unicode-0XFF01 + 0x9fa6-0x4e00;} offset * = 2; f_lseek (& utog_file, offset); // adjust the file pointer to the offset position if (f_read (& utog_file, buff, 2, & BW )! = Fr_ OK) // read 2 bytes {return 0x2020;} temp = buff [0]; temp <= 8; temp + = buff [1]; return temp; // return the code }///////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //// // # else // code table directly in the Code # include "unicode_gbk_code.h "/************** **************************************** **************************************** * *************************** function: u8 gbk_un Icode_init (void) * function: Initialize GBK, Unicode encoding table * parameter: none * return: 0: initialization successful; others: initialization failed * dependency: underlying read/write function * Author: cp1300@139.com * Time: 2013-04-18 * last modification time: 2013-04-18 * description: no *************************************** **************************************** **************************************** **/u8 gbk_unicode_init (void) {return 0 ;} /*************************************** **************************************** *********************** * ****************** Function: Maid function (gbkcode) * function: convert GBK encoding to unicode encoding * parameter: GBK * return: Unicode * dependency: underlying read/write function * Author: cp1300@139.com * Time: 20120602 * last modification time: 20120602 * description: the code table in flash must support the GBK code range. The height is 8 bits: 0x81 ~ 0xfe; low 8 bits: 0x40 ~ 0xfe *************************************** **************************************** **************************************** **/2010onegbktounicode (2010gbkcode) {u8 CH, Cl; CH = gbkcode> 8; CL = gbkcode & 0x00ff; ch-= 0x81; CL-= 0x40; return (CH <= 0x7d & CL <= 0xbe )? Wunicodes [CH * 0xbf + Cl]: 0x1fff ;} /*************************************** **************************************** **************************************** * ** function: feature: Convert Unicode to GBK encoding * parameter: Unicode * return: GBK * dependency: underlying read/write function * Author: cp1300@139.com * Time: 20120602 * last modification time: 20120602 * Note: the code table in Flash needs to support the GBK code range. The height is 8 bits: 0x81 ~ 0xfe; low 8 bits: 0x40 ~ 0xfe *************************************** **************************************** **************************************** **/service.eu-central-1.maxcompute.aliyun-inc.com/api) // use the binary search algorithm {u32 offset; 2010temp; If (UNICODE <= 0x9fa5) {If (UNICODE> = 0x4e00) offset = unicode-0X4E00; // 0x1b87 // 0x4e00, the starting point of the Chinese character offset elsereturn 0x2020; // the characters that cannot be displayed fill in two spaces; otherwise, garbled characters} else if (UNICODE> 0x9fa5) // It is a punctuation {If (UNICODE <0xff01 | Unicode> 0xff61) return 0x2020; // no corresponding Encoding Code // the characters that cannot be displayed fill with two spaces, otherwise garbled offset = unicode-0XFF01 + 0x9fa6-0x4e00;} offset * = 2; temp = wgbks [offset]; temp <= 8; temp + = wgbks [Offset + 1]; return temp; // return the code} # endif // gbk_unicode_is_sdcard /*************************** **************************************** **************************************** * ************** function: void gbktounicode (* pgbk, * punicode, u32 CNT) * function: Convert multiple GBK codes to Unicode * parameter: pgbk: GBK encoding slow Chong zone * punicode: uncode encoding buffer * CNT: Number of conversion encoding * returned: No * dependency: onegbktounicode * Author: cp1300@139.com * Time: 20130403 * last modification time: 20130403 * Note: the code table in flash must support the GBK code range. The height is 8 bits: 0x81 ~ 0xfe; low 8 bits: 0x40 ~ 0xfe *************************************** **************************************** **************************************** **/void gbktounicode (2010* pgbk, * punicode, u32 CNT) {While (CNT --) {* punicode = onegbktounicode (* pgbk ++); punicode ++ ;}} /*************************************** **************************************** **************************************** * ** function: void unicodetogbk (2010* Punic Ode, 2010* pgbk, u32 CNT) * function: Convert multiple Unicode codes to GBK * parameter: punicode: uncode encoding buffer * pgbk: GBK encoding buffer * CNT: number of conversion encoding * return: No * dependency: oneunicodetogbk * Author: cp1300@139.com * Time: 20130403 * last modification time: 20130403 * Note: The need for flash code table support GBK code range, high 8 bits: 0x81 ~ 0xfe; low 8 bits: 0x40 ~ 0xfe *************************************** **************************************** **************************************** **/void unicodetogbk (2010* punicode, * pgbk, u32 CNT) {While (CNT --) {* pgbk = oneunicodetogbk (* punicode ++); pgbk ++ ;}}

/*************************************** **************************************** * ****************************** File Name: unicode_gbk.h * function: Chinese character encoding conversion * Author: cp1300@139.com * Creation Time: * last modification time: * Details: you need to support ************************************* **************************************** * ******************************/# ifndef unicode_gbk_h _ # define unicode_gbk_h _ # include "system. H "u8 gbk_unicode_init (void); 2010onegbktounicode (2010gbkcode); 2010oneunicodetogbk (2010unicode); void gbktounicode (2010* pgbk, 2010* punicode, u32 CNT ); // convert multiple GBK codes to unicodevoid unicodetogbk (* punicode, * pgbk, u32 CNT ); // convert multiple Unicode codes to GBK # endif/* unicode_gbk_h _*/

Modify the Unicode supported files of cc963.c and fatfs

Modify the following function to comment out the two encoding tables in the file to save the flash of the single chip microcomputer.

# Include "unicode_gbk.h" // encode and convert wchar ff_convert (/* converted code, 0 means Conversion error */wcharsrc,/* character code to be converted */uintdir/* 0: unicode to oemcp, 1: oemcp to Unicode */) {wchar C; If (SRC <0x80) {/* ASCII */C = SRC ;} else {If (DIR) {/* oemcp to Unicode */C = onegbktounicode (SRC);} else {/* Unicode to oemcp */C = oneunicodetogbk (SRC );}} return C ;}# endif

In this way, as long as the fatfs is initialized, the code table can be initialized immediately to support the use of flash for long file names. This is not only used in fatfs, but also used in SMS encoding of the GSM module, decode the above.

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.