-
大小: 85KB文件類型: .rar金幣: 2下載: 0 次發布日期: 2021-06-08
- 語言: C/C++
- 標簽:
資源簡介
c/c++采用編碼轉換表實現gbk與unicode互轉,文件內含有兩個編碼大矩陣,
采用unicode小端模式,程序簡單易懂

代碼片段和文件信息
#include?
#include?“unicodeTables.h“
#include?“gbkTables.h“
using?namespace?std;
//gbk->unicode
int?GbkToUnicode(char?*gbk_buf?unsigned?short?*unicode_buf?int?max_unicode_buf_size)
{
????unsigned?short?word;
????unsigned?char?*gbk_ptr?=(unsigned?char?*)?gbk_buf;
????unsigned?short?*uni_ptr?=?unicode_buf;
unsigned?int?uni_ind?=?0?gbk_ind?=?0?uni_num?=?0;
unsigned?char?ch;
????int?word_pos;
????if(?!gbk_buf?||?!unicode_buf?)
????????return?-1;
????while(1)
????{
???? ch?=?*(gbk_ptr?+?gbk_ind);
if(ch?==?0x00)
break;
????????if(?ch?>?0x80?)
????????{
//將gbk編碼的中文字符的兩個字節組合成一個????unsigned?short?word;
word?=?*(gbk_ptr?+?gbk_ind);
word?<<=?8;
word?+=?*(gbk_ptr?+?gbk_ind+1);
gbk_ind?+=?2;
????????????word_pos?=?word?-?gbk_first_code;
????????????if(word?>=?gbk_first_code?&&?word?<=?gbk_last_code??&&?(word_pos?????????????{
*(uni_ptr?+?uni_ind)?=?unicodeTables[word_pos];
uni_ind++;
uni_num++;
????????????}
????????}
else
{
gbk_ind++;
*(uni_ptr?+?uni_ind)?=?ch;
uni_ind++;
uni_num++;
????????}
????????
????????if(uni_num?>?max_unicode_buf_size?-?1)
break;
????}
????return?uni_num;
}
//unicode->gbk
int?UnicodeToGbk(unsigned?short?*unicode_buf?char?*gbk_buf?int?max_gbk_buf_size)
{
unsigned?short?word;
unsigned?short?gbk_word;
unsigned?char?ch;
????unsigned?char?*gbk_ptr?=(unsigned?char?*)?gbk_buf;
????unsigned?short?*uni_ptr?=?unicode_buf;
unsigned?int?uni_ind?=?0?gbk_ind?=?0?gbk_num?=?0;
????int?word_pos;
????if(?!gbk_buf?||?!unicode_buf?)
????????return?-1;
while(1)
{
word?=?*(uni_ptr?+?uni_ind);
uni_ind++;
if(word?==?0x0000)??//字符串結束符
break;
if(word?0x80)??/*ASCII不用查表*/
{
*(gbk_ptr?+?gbk_ind)?=?(unsigned?char)word;
gbk_ind++;
}
else
{
word_pos?=?word?-?unicode_first_code;
if(word?>=?unicode_first_code?&&?word?<=?unicode_last_code?&&?word_pos? {
gbk_word?=?gbkTables[word_pos];//gbk_word是gbk編碼,但是為unsigned?short類型,需要拆分成兩個字節
*(gbk_ptr?+?gbk_ind)?=?(unsigned?char)(gbk_word?>>?8);//提取高8位
gbk_ind++;
*(gbk_ptr?+?gbk_ind)?=?(unsigned?char)(gbk_word?>>?0);//提取低8位
gbk_ind++;
gbk_num?+=2;//gbk字符加2個
}
}
if(gbk_num?>?max_gbk_buf_size?-?1)
break;
}
????return?gbk_num;
}
int?main()
{
////1、gbk->unicode
//char?*strGbk?=“我“;
//unsigned?short?strUnicode[5]?={0x00};
//
//?int?num?=?GbkToUnicode(strGbkstrUnicode5);??
?//
?//???unsigned?char*?p?=?(unsigned?char*)strUnicode;?????
?//???for?(int?i?=?0;?i??//???{??
?//???????printf(“%0x“?*p);??
?//???????p++;??
?//???}//輸出1162?
?//???printf(“\n“);??
//2、unicode->gbk
unsigned?short?strUnicode[2]?=?{0x62110x00};//加上0x00字符串結束符終止函數內部的循環
char?strGbk[5]={0x00};
int?num?=?UnicodeToGbk(strUnicodestrGbk5);
????unsigned?char*?p?=?(unsigned?char*)strGbk;?????
????for?(int?i?=?0;?
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????3211??2017-02-06?13:49??convert\convert.cpp
?????文件?????209234??2017-02-06?13:18??convert\gbkTables.h
?????文件?????287634??2017-02-06?13:22??convert\unicodeTables.h
?????目錄??????????0??2017-02-06?13:50??convert
-----------?---------??----------?-----??----
???????????????500079????????????????????4
- 上一篇:Qt5實現的一個mvc的
- 下一篇:MFC圖形編輯器源代碼
評論
共有 條評論