-
-
Notifications
You must be signed in to change notification settings - Fork 737
通过流进行字符集编码转换
ruki edited this page Aug 7, 2014
·
3 revisions
// 初始化文件输入流
tb_stream_ref_t istream = tb_stream_init_from_url("/home/utf8.txt");
// 初始化文件输出流
tb_stream_ref_t ostream = tb_stream_init_from_file("/home/gbk.txt", TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_BINARY | TB_FILE_MODE_TRUNC);
/* 初始化字符集编码流, 以istream作为输入, utf8 => gbk
*
* 目前支持的所有字符集编码格式:
*
* TB_CHARSET_TYPE_ASCII
* TB_CHARSET_TYPE_GB2312
* TB_CHARSET_TYPE_GBK
* TB_CHARSET_TYPE_ISO8859
* TB_CHARSET_TYPE_UCS2
* TB_CHARSET_TYPE_UCS4
* TB_CHARSET_TYPE_UTF16
* TB_CHARSET_TYPE_UTF32
* TB_CHARSET_TYPE_UTF8
*
* 注:针对多字节的编码格式,例如usc4, utf16, .. 默认转换用的是 大端格式
* 如果想要支持小端格式、本地端格式的数据, 可以传入:
*
* 小端格式数据: TB_CHARSET_TYPE_GBK | TB_CHARSET_TYPE_LE
*
* 本地端格式数据: TB_CHARSET_TYPE_UTF32 | TB_CHARSET_TYPE_NE
*/
tb_stream_ref_t fstream = tb_stream_init_filter_from_charset(istream, TB_CHARSET_TYPE_UTF8, TB_CHARSET_TYPE_GBK);
// 进行流编码
if (istream && ostream && fstream)
{
tb_hong_t save = tb_transfer_done(fstream, ostream, 0, tb_null, tb_null);
}
// 释放流数据
if (fstream) tb_stream_exit(fstream);
if (istream) tb_stream_exit(istream);
if (ostream) tb_stream_exit(ostream);