UNICODE to GB2312 pure C language Mapping method additional file Comment Area network disk

Source: Internet
Author: User
Tags 04x

The file used has a Unicode gb2312 mapping table   The default is from gb2312 to unicode   so the commented out part is the reverse order of the table. Don't spit out my slag sorting algorithm, Rush time   and it's win  slow down #include <stdint.h> #include  <stdbool.h>//#. Include <windows.h> #define  max_uni_index  6768//uint16_t const table_ gb2312_to_unicode[max_uni_index][2];//uint16_t const *const p_table_gb2312_to_unicode =  &table_gb2312_to_unicode[0][0];//uint16_t table_sorted_unicode[MAX_UNI_INDEX];//uint16_t  table_sorted_gb2312[max_uni_index];//uint16_t table_sorted_section_table[max_uni_index]; #if  0uint16_ T const sorted_unicode[max_uni_index];uint16_t const sorted_gb2312[max_uni_index];const  uint32_t c_un_sorted_value = 0xffffffff;uint32_t sort_result[max_uni_index];void  init_sort_result_buffer (void) {    for  (int i = 0; i  < max_uni_index; i++) {        sort_result[i] = c_un_sorted_value;    }}void sort_form_ Zero_to_end (void) {    uint16_t current_min_index = 0;     uint16_t min_tmp = c_un_sorted_value;    for  (int i =  0; i < max_uni_index; i++) {        for   (int j = 0; j < max_uni_index; j++) {             if  (Sort_result[j] == c_un_sorted_value) {//                 uint16_t tmp  = p_table_gb2312_to_unicode[2*j+1];    //sort by gb2312                 uint16_t tmp =  p_table_gb2312_to_unicode[2*j];        //sort by unicode                 if  (tmp < min_tmp) {                      current_min_index = j;                     min_tmp = tmp;                 }             }        }         sort_result[current_min_index] = i;         min_tmp = c_un_sorted_value;        current_min_index  = 0;     }}void get_group_unicode_gb2312 (void) {    for  (int i =  0; i<max_uni_index;i++) {        for  (int j  = 0; j < max_uni_index; j++) {             if  (sort_result[j] == i) {                 uint16_t tmp_gb2312 = p_table_ gb2312_to_unicode[2 * j + 1];                 uint8_t tmp_h_8 = tmp_gb2312 >> 8;                 uint8_t tmp_l_8  = tmp_gb2312 & 0xFF;                 //find sort                 table_sorted_unicode[i] = p_table_gb2312_to_unicode[2 * j];                 table_sorted_gb2312[i] = tmp_ gb2312;            }         }    }}void printf_to_file (void) {    int  File_write = fopen ("TEST.c", "w");     fprintf (File_write, "Uint16_t const  sorted_unicode[max_uni_index]]={\r\n ");    for  (int i = 0;  i < max_uni_index; i++) {        fprintf (File_write,   "0x%04x,\n",  table_sorted_unicode[i]);     }    fprintf (file _write,  "};\n\n"),     fprintf (file_write,  "uint16_t const sorted_gb2312[max_uni_index]]={\r\n" );    for  (int i = 0; i < max_uni_index; i++) {         fprintf (file_write,  "0x%04x,\n",  table_sorted_gb2312 [i]);     }    fprintf (file_write,  "};\n\n");     Fclose (file_write);} #endifint  quick_find (uint16_t const * p_data, uint16_t unicode, int  Min, int max) {    const int max_serch_cnt_ctrl = 15;     int ret = -2;    int mid;    int  i;    uint16_t cmp_min;    uint16_t cmp_max;     uint16_t cmp_mid;    for  (i = 0; i <  max_serch_cnt_ctrl; i++) {        cmp_min = p_ data[min];        cmp_max = p_data[max];         if  (Cmp_max == unicode) {             ret = max;             break;        }         else if  (Cmp_min == unicode) {             ret = min;             break;        }         else if  (max == min + 1) {             break;            //return -1;         }        mid =  (max +  min)  / 2;        cmp_mid = p_data[mid];         if  (Unicode > cmp_mid) {             min = mid;             continue;             //return quick_find (P_data, unicode, mid, max);         }        else{             max = mid;             continue;            //return quick_find (p_data,  Unicode, min, mid);        }    }     return ret;} uint16_t const sorted_unicode[max_uni_index];uint16_t const sorted_gb2312[max_uni_index]; Uint8_t buffer[32];void print_lib_font (uint8_t* pdata) {    for  (int  i = 0; i<16;i++) {        for  (int j  = 0; j < 2; j++) {             for  (int k = 0; k<8; k++) {                 uint8_t bit_fix = 0x80  >> k;                int tmp = pData[i*2 + j] & bit_fix;                 if  (TMP) {                     printf ("#");                 }                 else{                     printf ("-");                 }             }        }         printf ("\ n");     }}void test_lib (void ) {     int fp;//    uint16_t test_1 = 0x3158;    / /Port     uint16_t unicode = 0x68ee;    //unicode of   "Sen"     char c[] =  "one";     int result =  quick_find (sorted_unicode, unicode, 0, max_uni_index);     uint16_t  gb2312 = sorted_gb2312[result];    c[0] = gb2312 >>  8;    c[1] = gb2312 & 0xff;    uint8_t  h = c[0] - 0xA0;    uint8_t l = c[1] -  0xa0;    int t_1 =  (h - 1)  * 94 +  (l -  1))  * 32;//    t_1 = 32;    fp =  fopen ("HZK16",  "RB ");     fseek (fp, t_1, 0);     fread (Buffer, 32, 1, &NBSP;FP);     fclose (FP);     print_lib_font (buffer);} Int main (Int char_c, char** char_v) {    //init_sort_result_buffer ();     //sort_form_zero_to_end ();     //get_group_unicode_gb2312 ();     //printf_to_file ();     //{ 0x7f36, 0xf3be },//gb2312: Fou//     int resule = quick_find (Table_sorted_unicode, 0x7f36, 0,  max_uni_index - 1);//    printf ("\nfind_gb2312_is:0x%04x\nshould be  equal to 0xf3be ",  table_sorted_gb2312[resule]);//    printf (" Fou   Is  %x "," Fou ");     test_lib ();//    system (" Pause ");     return 0;}


UNICODE to GB2312 pure C language Mapping method additional file Comment Area network disk

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.