有没有比较好的utf8ios unicode转码码函数

第五章 常用Lua开发库2-JSON库、编码转换、字符串处理 - 开涛的博客(欢迎关注我的公众号[博客头像二维码]) - ITeye技术网站
博客分类:
在进行数据传输时JSON格式目前应用广泛,因此从Lua对象与JSON字符串之间相互转换是一个非常常见的功能;目前Lua也有几个JSON库,本人用过cjson、dkjson。其中cjson的语法严格(比如unicode \u0020\u7eaf),要求符合规范否则会解析失败(如\u002),而dkjson相对宽松,当然也可以通过修改cjson的源码来完成一些特殊要求。而在使用dkjson时也没有遇到性能问题,目前使用的就是dkjson。使用时要特别注意的是大部分JSON库都仅支持UTF-8编码;因此如果你的字符编码是如GBK则需要先转换为UTF-8然后进行处理。
1.1、test_cjson.lua
local cjson = require("cjson")
--lua对象到字符串
local obj = {
name = "zhangsan",
age = nil,
is_male = false,
hobby = {"film", "music", "read"}
local str = cjson.encode(obj)
ngx.say(str, "&br/&")
--字符串到lua对象
str = '{"hobby":["film","music","read"],"is_male":false,"name":"zhangsan","id":1,"age":null}'
local obj = cjson.decode(str)
ngx.say(obj.age, "&br/&")
ngx.say(obj.age == nil, "&br/&")
ngx.say(obj.age == cjson.null, "&br/&")
ngx.say(obj.hobby[1], "&br/&")
--循环引用
obj.obj = obj
-- Cannot serialise, excessive nesting
--ngx.say(cjson.encode(obj), "&br/&")
local cjson_safe = require("cjson.safe")
ngx.say(cjson_safe.encode(obj), "&br/&")
null将会转换为cjson.null;循环引用会抛出异常Cannot serialise, excessive nesting,默认解析嵌套深度是1000,可以通过cjson.encode_max_depth()设置深度提高性能;使用cjson.safe不会抛出异常而是返回nil。
1.2、example.conf配置文件
location ~ /lua_cjson {
default_type 'text/html';
content_by_lua_file /usr/example/lua/test_cjson.
1.3、访问如http://192.168.1.2/lua_cjson将得到如下结果
{"hobby":["film","music","read"],"is_male":false,"name":"zhangsan","id":1}
lua-cjson文档。
接下来学习下dkjson。
2.1、下载dkjson库
cd /usr/example/lualib/
wget http://dkolf.de/src/dkjson-lua.fsl/raw/dkjson.lua?name=16cbcda827df42cbeeb3 -O dkjson.lua
2.2、test_dkjson.lua
local dkjson = require("dkjson")
--lua对象到字符串
local obj = {
name = "zhangsan",
age = nil,
is_male = false,
hobby = {"film", "music", "read"}
local str = dkjson.encode(obj, {indent = true})
ngx.say(str, "&br/&")
--字符串到lua对象
str = '{"hobby":["film","music","read"],"is_male":false,"name":"zhangsan","id":1,"age":null}'
local obj, pos, err = dkjson.decode(str, 1, nil)
ngx.say(obj.age, "&br/&")
ngx.say(obj.age == nil, "&br/&")
ngx.say(obj.hobby[1], "&br/&")
--循环引用
obj.obj = obj
--reference cycle
--ngx.say(dkjson.encode(obj), "&br/&")
默认情况下解析的json的字符会有缩排和换行,使用{indent = true}配置将把所有内容放在一行。和cjson不同的是解析json字符串中的null时会得到nil。
2.3、example.conf配置文件
location ~ /lua_dkjson {
default_type 'text/html';
content_by_lua_file /usr/example/lua/test_dkjson.
2.4、访问如http://192.168.1.2/lua_dkjson将得到如下结果
{ "hobby":["film","music","read"], "is_male":false, "name":"zhangsan", "id":1 }
dkjson文档和。
我们在使用一些类库时会发现大部分库仅支持UTF-8编码,因此如果使用其他编码的话就需要进行编码转换的处理;而Linux上最常见的就是iconv,而lua-iconv就是它的一个Lua API的封装。
安装lua-iconv可以通过如下两种方式:
ubuntu下可以使用如下方式
apt-get install luarocks
luarocks install lua-iconv
cp /usr/local/lib/lua/5.1/iconv.so
/usr/example/lualib/
源码安装方式,需要有gcc环境
wget /do^Cloads/ittner/lua-iconv/lua-iconv-7.tar.gz
tar -xvf lua-iconv-7.tar.gz
cd lua-iconv-7
gcc -O2 -fPIC -I/usr/include/lua5.1 -c luaiconv.c -o luaiconv.o -I/usr/include
gcc -shared -o iconv.so -L/usr/local/lib luaiconv.o -L/usr/lib
cp iconv.so
/usr/example/lualib/
1、test_iconv.lua
ngx.say("中文")
此时文件编码必须为UTF-8,即Lua文件编码为什么里边的字符编码就是什么。
2、example.conf配置文件
location ~ /lua_iconv {
default_type 'text/html';
content_by_lua_file /usr/example/lua/test_iconv.
通过charset告诉浏览器我们的字符编码为gbk。
3、访问 http://192.168.1.2/lua_iconv会发现输出乱码;
此时需要我们将test_iconv.lua中的字符进行转码处理:
local iconv = require("iconv")
local togbk = iconv.new("gbk", "utf-8")
local str, err = togbk:iconv("中文")
ngx.say(str)
通过转码我们得到最终输出的内容编码为gbk, 使用方式iconv.new(目标编码, 源编码)。
有如下可能出现的错误:
没有错误成功。
iconv.ERROR_NO_MEMORY
内存不足。
iconv.ERROR_INVALID
有非法字符。
iconv.ERROR_INCOMPLETE
有不完整字符。
iconv.ERROR_FINALIZED
使用已经销毁的转换器,比如垃圾回收了。
iconv.ERROR_UNKNOWN
iconv在转换时遇到非法字符或不能转换的字符就会失败,此时可以使用如下方式忽略转换失败的字符
local togbk_ignore = iconv.new("GBK//IGNORE", "UTF-8")
另外在实际使用中进行UTF-8到GBK转换过程时,会发现有些字符在GBK编码表但是转换不了,此时可以使用更高的编码GB18030来完成转换。
更多介绍请参考。
Lua 5.3之前是没有提供位运算支持的,需要使用第三方库,比如LuaJIT提供了bit库。
1、test_bit.lua
local bit = require("bit")
ngx.say(bit.lshift(1, 2))
lshift进行左移位运算,即得到4。
其他位操作API请参考。Lua 5.3的位运算操作符.
ngx_lua模块本身提供了全局共享内存ngx.shared.DICT可以实现全局共享,另外可以使用如Redis来实现缓存。另外还一个实现,其和ngx.shared.DICT不一样的是它是每Worker进程共享,即每个Worker进行会有一份缓存,而且经过实际使用发现其性能不如ngx.shared.DICT。但是其好处就是不需要进行全局配置。
1、创建缓存模块来实现只初始化一次:
vim /usr/example/lualib/mycache.lua
local lrucache = require("resty.lrucache")
--创建缓存实例,并指定最多缓存多少条目
local cache, err = lrucache.new(200)
if not cache then
ngx.log(ngx.ERR, "create cache error : ", err)
local function set(key, value, ttlInSeconds)
cache:set(key, value, ttlInSeconds)
local function get(key)
return cache:get(key)
local _M = {
set = set,
此处利用了模块的特性实现了每个Worker进行只初始化一次cache实例。
2、test_lrucache.lua
local mycache = require("mycache")
local count = mycache.get("count") or 0
count = count + 1
mycache.set("count", count, 10 * 60 * 60) --10分钟
ngx.say(mycache.get("count"))
可以实现诸如访问量统计,但仅是每Worker进程的。
3、example.conf配置文件
location ~ /lua_lrucache {
default_type 'text/html';
content_by_lua_file /usr/example/lua/test_lrucache.
访问如http://192.168.1.2/lua_lrucache测试。
更多介绍请参考。
字符串处理
Lua 5.3之前没有提供字符操作相关的函数,如字符串截取、替换等都是字节为单位操作;在实际使用时尤其包含中文的场景下显然不能满足需求;即使Lua 5.3也仅提供了基本的。
Lua UTF-8库
/starwing/luautf8
LuaRocks安装
#首先确保git安装了
apt-get install git
luarocks install utf8
cp /usr/local/lib/lua/5.1/utf8.so
/usr/example/lualib/
wget /starwing/luautf8/archive/master.zip
unzip master.zip
cd luautf8-master/
gcc -O2 -fPIC -I/usr/include/lua5.1 -c utf8.c -o utf8.o -I/usr/include
gcc -shared -o utf8.so -L/usr/local/lib utf8.o -L/usr/lib
1、test_utf8.lua
local utf8 = require("utf8")
local str = "abc中文"
ngx.say("len : ", utf8.len(str), "&br/&")
ngx.say("sub : ", utf8.sub(str, 1, 4))
文件编码必须为UTF8,此处我们实现了最常用的字符串长度计算和字符串截取。
2、example.conf配置文件
location ~ /lua_utf8 {
default_type 'text/html';
content_by_lua_file /usr/example/lua/test_utf8.
3、访问如http://192.168.1.2/lua_utf8测试得到如下结果
len : 5sub : abc中
字符串转换为unicode编码:
local bit = require("bit")
local bit_band = bit.band
local bit_bor = bit.bor
local bit_lshift = bit.lshift
local string_format = string.format
local string_byte = string.byte
local table_concat = table.concat
local function utf8_to_unicode(str)
if not str or str == "" or str == ngx.null then
return nil
local res, seq, val = {}, 0, nil
for i = 1, #str do
local c = string_byte(str, i)
if seq == 0 then
if val then
res[#res + 1] = string_format("%04x", val)
seq = c & 0x80 and 1 or c & 0xE0 and 2 or c & 0xF0 and 3 or
c & 0xF8 and 4 or --c & 0xFC and 5 or c & 0xFE and 6 or
if seq == 0 then
ngx.log(ngx.ERR, 'invalid UTF-8 character sequence' .. ",,," .. tostring(str))
return str
val = bit_band(c, 2 ^ (8 - seq) - 1)
val = bit_bor(bit_lshift(val, 6), bit_band(c, 0x3F))
seq = seq - 1
if val then
res[#res + 1] = string_format("%04x", val)
if #res == 0 then
return str
return "\\u" .. table_concat(res, "\\u")
ngx.say("utf8 to unicode : ", utf8_to_unicode("abc中文"), "&br/&")
如上方法将输出utf8 to unicode : \u\ud\u6587。
删除空格:
local function ltrim(s)
if not s then
local res = s
local tmp = string_find(res, '%S')
if not tmp then
elseif tmp ~= 1 then
res = string_sub(res, tmp)
return res
local function rtrim(s)
if not s then
local res = s
local tmp = string_find(res, '%S%s*$')
if not tmp then
elseif tmp ~= #res then
res = string_sub(res, 1, tmp)
return res
local function trim(s)
if not s then
local res1 = ltrim(s)
local res2 = rtrim(res1)
return res2
字符串分割:
function split(szFullString, szSeparator)
local nFindStartIndex = 1
local nSplitIndex = 1
local nSplitArray = {}
while true do
local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex)
if not nFindLastIndex then
nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, string.len(szFullString))
nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, nFindLastIndex - 1)
nFindStartIndex = nFindLastIndex + string.len(szSeparator)
nSplitIndex = nSplitIndex + 1
return nSplitArray
如split("a,b,c", ",") 将得到一个分割后的table。
到此基本的字符串操作就完成了,其他luautf8模块的API和LuaAPI类似可以参考
另外对于GBK的操作,可以先转换为UTF-8,最后再转换为GBK即可。
浏览 10419
开-涛-哥真厉害,一直走在技术的最前端。彻底膜拜!请问一个问题:在使用同样的库的条件下,c语言算出来的文件crc32 与javascript算出来是否不一致呢?&& 不回复也没关系哈这个没做过,可以找相同的算法库,或者是不是因为字符集的问题
jinnianshilongnian
浏览: 6861859 次
浏览量:1052340
浏览量:1160610
浏览量:2135875
浏览量:122752
浏览量:515744
浏览量:137635
浏览量:1159171
浏览量:141702
多谢楼主分享,学习了,不错
请问下文中所说的路由的默认最大连接是2,这个值是在源码的哪个位 ...
写得太棒了,专门注册个账号来赞一下!php实现utf-8转unicode函数代码实例-Php常用代码-Php教程-壹聚教程网php实现utf-8转unicode函数代码实例最近做的项目要实现跨平台,项目用的是utf-8的编码,由于在操作过程中出现各种编码问题,于是就把utf-8编码转成能够使计算机实现跨语言、跨平台的文本转换及处理的unicode编码。
对于这种问题,我们还是直接上代码来的快,大伙拿去直接用吧,不要谢我。
public function utf8_unicode($str) {
&&& $unicode = array();
&&& $values = array();
&&& $lookingFor = 1;
&&& for ($i = 0; $i & strlen( $str ); $i++ ) {
&&&&&&& $thisValue = ord( $str[ $i ] );
&&&&&&& if ( $thisValue & ord('A') ) {
&&&&&&&&&&& // exclude 0-9
&&&&&&&&&&& if ($thisValue &= ord('0') && $thisValue &= ord('9')) {
&&&&&&&&&&&&&&&& // number
&&&&&&&&&&&&&&&& $unicode[] = chr($thisValue);
&&&&&&&&&&& }
&&&&&&&&&&& else {
&&&&&&&&&&&&&&&& $unicode[] = '%'.dechex($thisValue);
&&&&&&&&&&& }
&&&&&&& } else {
&&&&&&&&&&& if ( $thisValue & 128) {
&&&&&&&&&&&&&&& $unicode[] = $str[ $i ];
&&&&&&&&&&& } else {
&&&&&&&&&&&&&&& if ( count( $values ) == 0 ) {
&&&&&&&&&&&&&&&&&&& $lookingFor = ( $thisValue & 224 ) ? 2 : 3;
&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&& $values[] = $thisV
&&&&&&&&&&&&&&& if ( count( $values ) == $lookingFor ) {
&&&&&&&&&&&&&&&&&&& $number = ( $lookingFor == 3 ) ?
&&&&&&&&&&&&&&&&&&&&&&& ( ( $values[0] % 16 ) * 4096 ) + ( ( $values[1] % 64 ) * 64 ) + ( $values[2] % 64 ):
&&&&&&&&&&&&&&&&&&&&&&& ( ( $values[0] % 32 ) * 64 ) + ( $values[1] % 64 );
&&&&&&&&&&&&&&&&&&& $number = dechex($number);
&&&&&&&&&&&&&&&&&&& $unicode[] = (strlen($number)==3)?&u0&.$number:&u&.$
&&&&&&&&&&&&&&&&&&& $values = array();
&&&&&&&&&&&&&&&&&&& $lookingFor = 1;
&&&&&&&&&&&&&&& } // if
&&&&&&&&&&& } // if
&&& } // for
&&& return implode(&&,$unicode);
} 上一页: &&&&&下一页:相关内容
儿童安全乘车与修理梦西游编辑推荐
电脑壁纸相关专题有关unicode,UTF-8及 ANSI等编码转换_最火下载站
您的位置: >
> 有关unicode,UTF-8及 ANSI等编码转换
有关unicode,UTF-8及 ANSI等编码转换
1. ANSI 美国国家标准码,操作系统默认的编码格式;但是不同国家有不同的文字,由此各个国家制定了自己的国标码,如GB2312等。注意,各个国家制定时还是按照ANSI准则进行的:即不属于ASCII(0~127)的一个文字(符号)占两个字节,属于ASCII的占一个字节。这样一来,一个大字符串用不同国标码,解释的结果就不同(但一点就是,属于ASCII部分的字符解释出来是相同的)。 ANSI是编码,GB2312等国标码是符合ANSI标准的字符集。字符集与编码是两回事。 2. 字符 字符是一个符号,如:'#','◎'等,存储时根据不同的编码标准可能占用1个或多个字节的空间。不同编码标准下,字符占用空间大小不同,如Unicode编码所有字符都是2个字节,utf-8编码占用从1个到6个不等。 3. 多字节字符串: 字符串在内存中,如果&字符&是以ANSI编码形式存放的,则一个字符可能使用一个字节或多个字节来表示,称这个字符串为ANSI字符串或多字节字符串。 4. Unicode: 统一码,任何字符都占两个字节。各个国家文字、符号统一编码。 VC++或其他编程工具对汉字或字符都采用操作系统的编码标准,一般都是ANSI标准。这就涉及往其它编码转化的问题。 5. ANSI与Unicode 如果ANSI全部属于ASCII(0~127),则 mbstowcs, wcstombs 即可。因为Unicode对ASCII(0~127)的处理是&直接扩展ANSI&--由一个字节到两个字节。 size_t mbstowcs( wchar_t *wcstr, const char *mbstr, size_t count ); size_t wcstombs( char *mbstr, const wchar_t *wcstr, size_t count );对于不属于ASCII的,如汉字,使用 WideCharToMultiByte,MultiByteToWideChar。 int MultiByteToWideChar( UINT CodePage, // code page DWORD dwFlags, // character-type options LPCSTR lpMultiByteStr, // string to map int cbMultiByte, // number of bytes in string LPWSTR lpWideCharStr, // wide-character buffer int cchWideChar // size of buffer ); 6. 多字节到宽字符(Unicode) 待转换的字符并不一定是多字节字符串(ANSI字符串),uft-8编码的也可以。 CodePage:待转换的代码页,如CP_ACP(ANSI),utf-8; dwFlags: 0; lpMultiByteStr,cbMultiByte:待转换 lpWideCharStr,cchWideChar:转换完 int WideCharToMultiByte( UINT CodePage, // code page DWORD dwFlags, // performance and mapping flags LPCWSTR lpWideCharStr, // wide-character string int cchWideChar, // number of chars in string LPSTR lpMultiByteStr, // buffer for new string int cbMultiByte, // size of buffer LPCSTR lpDefaultChar, // default for unmappable chars LPBOOL lpUsedDefaultChar // set when default char used ); 7. 宽字符(Unicode)到多字节 新字符串不必是多字节(ANSI)字符集。 CodePag:要转换成的代码页,如CP_ACP(ANSI),utf-8; dwFlags:0; lpWideCharStr,cchWideChar:待转换 lpMultiByteStr,cbMultiByte:转换完 lpDefaultChar,lpUsedDefaultChar:失败时缺省字符; 一个字符串中有utf-8,如何转换成ANSI? 首先,utf-8 到 Unicode 其次,Unicode 到 ANSI 代码如下: int ConvUtf8ToAnsi(CString& strSource, CString& strChAnsi) { if (strSource.GetLength() &= 0) return 0; CString strWChU strSource.TrimLeft(); strSource.TrimRight(); strChAnsi.Empty(); int iLenByWChNeed = MultiByteToWideChar(CP_UTF8, 0, strSource.GetBuffer(0), strSource.GetLength(), NULL, 0); int iLenByWchDone = MultiByteToWideChar(CP_UTF8, 0, strSource.GetBuffer(0), strSource.GetLength(), (LPWSTR)strWChUnicode.GetBuffer(iLenByWChNeed * 2), iLenByWChNeed); strWChUnicode.ReleaseBuffer(iLenByWchDone * 2); int iLenByChNeed = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)strWChUnicode.GetBuffer(0), iLenByWchDone, NULL, 0, NULL, NULL); int iLenByChDone = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)strWChUnicode.GetBuffer(0), iLenByWchDone, strChAnsi.GetBuffer(iLenByChNeed), iLenByChNeed, NULL, NULL); strChAnsi.ReleaseBuffer(iLenByChDone); if (iLenByWChNeed != iLenByWchDone || iLenByChNeed != iLenByChDone) return 1; return 0; } 8. Unicode 到 ANSI 其它方法 1. 调用CRT 函数wcstombs(); 2. 使用CString 构造器或赋值操作(仅用于MFC ); 3. 使用ATL 串转换宏; size_t wcstombs ( char* mbstr, // 接受结果ANSI串的字符(char)缓冲。 const wchar_t* wcstr, // 要转换的Unicode串。 size_t count ); // mbstr参数所指的缓冲大小。 MFC中的CString包含有构造函数和接受Unicode串的赋值操作,所以你可以用CString来实现转换。 // 假设有一个Unicode串wszSomeString... CString str1 ( wszSomeString ); // 用构造器转换 CString str2; str2 = wszSomeS // 用赋值操作转换 ATL有一组很方便的宏用于串的转换。W2A()用于将Unicode串转换为ANSI串(记忆方法是&wide to ANSI&&&宽字符到ANSI)。实际上使用OLE2A()更精确,&OLE&表示的意思是COM串或者OLE串。 9. VC6 IDE下查看Unicode字符的方法 unicode字符后面加&,su&
上一篇: 下一篇:Unicode字符编码转换器在各种字符编码(包括UTF-8/UTF-16/中文简体GBK/繁体BIG5/日文SHIFT-JIS/韩文euc-kr等)之间进行批量转换
Unicode字符编码转换器 V2.0
又被称为 &&
(只能运行30次, 1mb)
Unicode字符编码转换器, 又被称为&&或者&&,
是一款易于使用的Unicode转换程序, 它帮助您在各种字符编码(ansi/Unicode/非Unicode)之间进行批量转换,
它支持Unicode(UTF-8/UTF-16/UTF-7/UTF-32), 中文简体GBK, 中文繁体BIG5,
日文SHIFT-JIS, 日文EUC-JP, 韩文euc-kr等字符编码,
它可以几分钟内同时批量处理几千个文档。
它能够转换非Unicode到Unicode, 例如, 转换中文简体GBK和中文繁体BIG5到Unicode,
转换日文SHIFT-JIS/EUC-JP到Unicode, 转换iso8859-1字符编码到Unicode,
并且转换 ansi到Unicode等.
它也能够从Unicode转换到非Unicode, 例如, 转换Unicode到中文简体GBK,
转换Unicode到中文繁体BIG5, 转换Unicode到日文SHIFT-JIS/EUC-JP, 转换Unicode到iso8859-1
字符编码, 并且转换Unicode到ansi等.
它也能够在uncode之间进行转换, 例如, 转换UTF-16到UTF-8, 转换UTF-8到UTF-16.
Unicode字符编码转换器只做文本文件转换, 例如, 它能够转换 .txt文本文件, .php文件, .xml文件, .html文件等.
值得注意的是, 它不是一个文本格式转换器! 例如不能够转换PDF到text文本文件或者其他格式.
在文本字符编码之间进行转换(包括UTF-8,没有bom标记的UTF-8,UTF-16le,UTF-16be,UTF-32le,UTF-32be,中文简体GBK,中文繁体BIG5,日文shift-JIS,
日文euc-jp, 韩文 euc-kr等, 请参照),
转换中文简体GBK到Unicode,
中文繁体BIG5转换到Unicode,&
转换日文SHIFT-JIS/EUC-JP到Unicode,&
转换iso8859-1到Unicode,&
转换ansi到Unicode,&
转换Unicode到中文简体GBK,
转换Unicode到中文繁体BIG5,
转换Unicode到日文SHIFT-JIS/EUC-JP,
转换Unicode到iso8859-1,
转换Unicode到ansi,
转换UTF-16到UTF-8,
转换UTF-8到UTF-16等.
快速批量转换很多文件, 支持子目录, 它能够几分钟内同时批量处理几千个文档!
能够选择输出DOS (CR/LF), Unix (LF), Mac (CR)行格式,
或者选择不转换原行格式.
您能够通过文件过滤器、在文件名中或者文件路径中包括字符串、在文件名中或者文件路径中排除字符串来对文件进行过滤。
自动检测源文件编码格式或者指定源文件编码格式。
这样你就能够在你的应用中进行调用。
多语言界面支持, 现在包括英文、中文简体GBK和中文繁体BIG5。
Western European (ISO-8859-1)
Central European (ISO-8859-2)
Esperanto (ISO-8859-3)
Baltic (old) (ISO-8859-4)
Cyrillic (ISO-8859-5)
Arabic (ISO-8859-6)
Greek (ISO-8859-7)
Hebrew (ISO-8859-8)
Turkish (ISO-8859-9)
Nordic (ISO-8859-10)
Thai (ISO-8859-11)
Baltic (ISO-8859-13)
Celtic (ISO-8859-14)
Western European with Euro (ISO-8859-15)
Windows Thai (CP 874)
日文 SHIFT-JIS (CP932)
中文简体 GBK (CP936)
韩文 EUC-KR (CP949)
中文繁体 BIG5 (CP950)
Windows Central European (CP 1250)
Windows Cyrillic (CP 1251)
Windows Western European (CP 1252)
Windows Greek (CP 1253)
Windows Turkish (CP 1254)
Windows Hebrew (CP 1255)
Windows Arabic (CP 1256)
Windows Baltic (CP 1257)
Windows/DOS OEM (CP 437)
Unicode 7 bit (UTF-7)
Unicode 8 bit (UTF-8)
Unicode 8 bit (UTF-8) NO BOM
日文 EUC-JP
发布Unicode字符编码转换器v1.0 build100827, 增加,
谢谢Thomas Jensen的建议。
发布Unicode字符编码转换器v1.0 build100525, 增加没有BOM标记的UTF-8编码到目的文件编码列表中,
在Unicode字符编码转换器界面上,
你只要在目的文件编码列表中选择&Unicode 8 bit (UTF-8) NO
BOM&, 然后进行转换, 最好你就能够得到没有 BOM标记的
UTF-8编码文件了, 谢谢Gert Van Assche的主意.
发布Unicode字符编码转换器v1.0 build091020, 修复了当选择&转换文件到同一路径&时发生的错误.
发布Unicode字符编码转换器V1.0 build091009.
至少需要一台Pentium 150计算机, 16MB更多内存.&
操作系统为Windows 95/98/ME/2000/NT 4.X/XP/VISTA
自动检测源文件编码格式,转换非Unicode到Unicode UTF-8,
并且不转换行格式
不转换编码格式, 只转换行格式到dosc中实现utf8和gbk的互转 -
- ITeye技术网站
博客分类:
#include &iconv.h&
#include &stdlib.h&
#include &stdio.h&
#include &unistd.h&
#include &fcntl.h&
#include &string.h&
#include &sys/stat.h&
int code_convert(char *from_charset, char *to_charset, char *inbuf, size_t inlen,
char *outbuf, size_t outlen) {
char **pin = &
char **pout = &
cd = iconv_open(to_charset, from_charset);
if (cd == 0)
return -1;
memset(outbuf, 0, outlen);
if (iconv(cd, pin, &inlen, pout, &outlen) == -1)
return -1;
iconv_close(cd);
*pout = '\0';
int u2g(char *inbuf, size_t inlen, char *outbuf, size_t outlen) {
return code_convert("utf-8", "gb2312", inbuf, inlen, outbuf, outlen);
int g2u(char *inbuf, size_t inlen, char *outbuf, size_t outlen) {
return code_convert("gb2312", "utf-8", inbuf, inlen, outbuf, outlen);
int main(void) {
char *s = "中国";
int fd = open("test.txt", O_RDWR|O_CREAT, S_IRUSR | S_IWUSR);
char buf[10];
u2g(s, strlen(s), buf, sizeof(buf));
write(fd, buf, strlen(buf));
close(fd);
fd = open("test.txt2", O_RDWR|O_CREAT, S_IRUSR | S_IWUSR);
char buf2[10];
g2u(buf, strlen(buf), buf2, sizeof(buf2));
write(fd, buf2, strlen(buf2));
close(fd);
上面是使用iconv函数。
方式二: 使用如下两个函数
mbstowcs将多字节编码转换为宽字节编码
wcstombs将宽字节编码转换为多字节编码
注意, 需要系统编码的支持, 可以通过locale -a 查看系统支持的。若不支持zh_CN.gbk, 需要安装,例如,在ubuntu上的安装步骤如下:
$sudo vi /var/lib/locales/supported.d/zh-hans
zh_CN.UTF-8 UTF-8
zh_SG.UTF-8 UTF-8
zh_CN.GBK GBK
zh_CN.GB18030 GB18030
$ sudo locale-gen
$ locale -a
zh_CN.gb18030
zh_CN.utf8
zh_SG.utf8
#include &stdlib.h&
#include &stdio.h&
#include &string.h&
#include &unistd.h&
#include &fcntl.h&
#include &sys/stat.h&
#include &locale.h&
* DESCRIPTION: 实现由utf8编码到gbk编码的转换
* Input: gbkStr,转换后的字符串;
srcStr,待转换的字符串; maxGbkStrlen, gbkStr的最
* Output: gbkStr
* Returns: -1,&0,success
int utf82gbk(char *gbkStr, const char *srcStr, int maxGbkStrlen) {
if (NULL == srcStr) {
printf("Bad Parameter\n");
return -1;
//首先先将utf8编码转换为unicode编码
if (NULL == setlocale(LC_ALL, "zh_CN.utf8")) //设置转换为unicode前的码,当前为utf8编码
printf("Bad Parameter\n");
return -1;
int unicodeLen = mbstowcs(NULL, srcStr, 0); //计算转换后的长度
if (unicodeLen &= 0) {
printf("Can not Transfer!!!\n");
return -1;
wchar_t *unicodeStr = (wchar_t *) calloc(sizeof(wchar_t), unicodeLen + 1);
mbstowcs(unicodeStr, srcStr, strlen(srcStr)); //将utf8转换为unicode
//将unicode编码转换为gbk编码
if (NULL == setlocale(LC_ALL, "zh_CN.gbk")) //设置unicode转换后的码,当前为gbk
printf("Bad Parameter\n");
return -1;
int gbkLen = wcstombs(NULL, unicodeStr, 0); //计算转换后的长度
if (gbkLen &= 0) {
printf("Can not Transfer!!!\n");
return -1;
} else if (gbkLen &= maxGbkStrlen) //判断空间是否足够
printf("Dst Str memory not enough\n");
return -1;
wcstombs(gbkStr, unicodeStr, gbkLen);
gbkStr[gbkLen] = 0; //添加结束符
free(unicodeStr);
return gbkL
int main(void) {
char *s = "中国";
int fd = open("test.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
char buf[10];
utf82gbk(buf, s, sizeof(buf));
write(fd, buf, strlen(buf));
close(fd);
浏览: 183173 次
来自: 杭州
nk_tocean 写道照着做了,但是不行啊,还是乱码.先确认 ...
照着做了,但是不行啊,还是乱码.
您好,能不能把语言包给我发过来,我找不到。谢谢 1790958 ...
修改配置路径到 JDK 安装目录下的 jre 亦可
搂主是正确的,刚刚招到原因,我自己写了一个serde,里面用了 ...}

我要回帖

更多关于 unicode js 转码 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信