資源簡介
純C語音進行各種格式編碼直接的轉換。
int gbk_to_unicode
int unicode_to_gbk
int unicode_to_utf8
int utf8_to_unicode

代碼片段和文件信息
#include?“stdafx.h“
#include?“string_conv.h“
#include?“gbk_to_unicode.h“
#include?“unicode_to_gbk.h“
int?dprintf(char?*format?...?)
{
#if?0
char?buf[1024];
va_list?marker;
int?n;
va_start(marker?format); //?開始分析字符串
n?=?vsprintf(buf?format?marker); //?格式化輸出字符串
printf(“%s“?buf);
va_end(marker); //?結束分析
return?0;
#else
return?0;
#endif
}
/*-----------------+-------------------------+------------------------
???????????????????|????endian?=?1?大端??????|
???|????endian?=?0?小端??????|
-------------------+-------------------------+-----------------------*/
int?gbk_to_unicode(unsigned?char?*gbk_buf?unsigned?short?*unicode_buf?int?max_unicode_buf_size?int?endian)
{
????unsigned?short?word;
????unsigned?char?*gbk_ptr?=?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?)
????????{
????????????//chinese
????????????//word?=?*p?*256?+?*(p+1);
if(endian?==?1)??//大端
{
word?=?*(gbk_ptr?+?gbk_ind);
word?<<=?8;
gbk_ind++;
word?+=?*(gbk_ptr?+?gbk_ind);
gbk_ind++;
}
else
{
word?=?*(gbk_ptr?+?gbk_ind?+?1);
word?<<=?8;
word?+=?*(gbk_ptr?+?gbk_ind);
gbk_ind?+=?2;
}
//printf(“word:?0x%x\n“?word);
????????????word_pos?=?word?-?g_gbk_first_code;
????????????if(word?>=?g_gbk_first_code?&&?word?<=?g_gbk_last_code??&&?(word_pos?????????????{
????????????????//*p_out?=?g_gbk_to_unicode_buf[word_pos];
????????????????//*p_out?++;
????????????????//n++;
*(uni_ptr?+?uni_ind)?=?g_gbk_to_unicode_buf[word_pos];
uni_ind++;
uni_num++;
????????????}
????????}
else
{
????????????//for?ASCII
????????????//*p_out?=?(unsigned?short)*p;
????????????//*p_out?++;
????????????//n++;
gbk_ind++;
//*(uni_ptr?+?uni_ind)?=?g_gbk_to_unicode_buf[word_pos];
*(uni_ptr?+?uni_ind)?=?ch;
uni_ind++;
uni_num++;
????????}
????????
????????if(uni_num?>?max_unicode_buf_size?-?1)
break;
????}
????return?uni_num;
}
int?unicode_to_gbk(unsigned?short?*unicode_buf?unsigned?char?*gbk_buf?int?max_gbk_buf_size?int?endian)
{
unsigned?short?word;
unsigned?short?gbk_word;
unsigned?char?ch;
????unsigned?char?*gbk_ptr?=?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;
//dprintf(“[0]word:?0x%x\n“?word);
if(endian?==?1)??//大端
{
//高低位交換
ch?=?(unsigned?char)word;
word?=?word?>>?8;
word?+=?ch?<8;
}
//dprintf(“
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????287902??2010-07-22?11:51??conver?code\gbk_to_unicode.h
?????文件???????7318??2010-07-22?14:55??conver?code\string_conv.cpp
?????文件????????929??2010-07-22?14:52??conver?code\string_conv.h
?????文件?????209545??2010-07-22?14:48??conver?code\unicode_to_gbk.h
?????目錄??????????0??2010-07-22?14:56??conver?code
-----------?---------??----------?-----??----
???????????????505694????????????????????5
- 上一篇:數據結構與算法 C語言描述課后習題答案
- 下一篇:下行法求故障樹最小割集
評論
共有 條評論