首页 > 代码库 > UNICODE 转GB2312 纯C语言 映射法 另外的文件评论区网盘
UNICODE 转GB2312 纯C语言 映射法 另外的文件评论区网盘
//使用到的文件有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; }
UNICODE 转GB2312 纯C语言 映射法 另外的文件评论区网盘
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。