//使用到的文件有unicode转gb2312的映射表 默认是从gb2312转unicode 所以注释掉的部分是把表反序。 //不要吐槽我的渣渣排序算法,赶时间的 而且是WIN 慢点就慢点 #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 0 uint16_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); } #endif int 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; //口 uint16_t unicode = 0x68EE; //UNICODE OF "森" char c[] = "一"; 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, 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:缶 // 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("缶 is %x","缶"); test_lib(); // system("pause"); return 0; }
时间: 2024-10-26 11:07:33