纯lua实现 utf-16le 和 utf-8互转
来源:互联网 发布:linux 本地socket通信 编辑:程序博客网 时间:2024/04/29 00:01
原帖地址:http://www.cocoachina.com/bbs/read.php?tid-312194.html
做了一些小小的修改:
do local bit = require("bit") local resultStr={} local function utf16le_to_utf8(convertStr) if type(convertStr)~="string" then return convertStr end local i=1 local len = 1; while true do local num1=string.byte(convertStr,i) local unicode; if num1 ~= nil then local num2=string.byte(convertStr,i+1) unicode = bit.bor(num1, bit.lshift(num2, 8)); i=i+2; else break; end -- print(unicode) if unicode <= 0x007f then resultStr[len] = string.char(bit.band(unicode,0x7f)) len = len + 1; elseif unicode >= 0x0080 and unicode <= 0x07ff then resultStr[len] = string.char(bit.bor(0xc0,bit.band(bit.rshift(unicode,6),0x1f))) len = len + 1; resultStr[len] = string.char(bit.bor(0x80,bit.band(unicode,0x3f))) len = len + 1; elseif unicode >= 0x0800 and unicode <= 0xffff then resultStr[len] = string.char(bit.bor(0xe0,bit.band(bit.rshift(unicode,12),0x0f))) len = len + 1; resultStr[len] = string.char(bit.bor(0x80,bit.band(bit.rshift(unicode,6),0x3f))) len = len + 1; resultStr[len] = string.char(bit.bor(0x80,bit.band(unicode,0x3f))) len = len + 1; end end -- resultStr[len] = '\0' -- print(resultStr) return table.concat(resultStr, "", 1, len-1) end local function utf8_to_utf16le(convertStr) if type(convertStr)~="string" then return convertStr end local i=1 local len = 1; local num1=string.byte(convertStr,i) while num1~=nil do -- print(num1) local tempVar1,tempVar2 if num1 >= 0x00 and num1 <= 0x7f then tempVar1=num1 tempVar2=0 elseif bit.band(num1,0xe0)== 0xc0 then local t1 = 0 local t2 = 0 t1 = bit.band(num1,bit.rshift(0xff,3)) i=i+1 num1=string.byte(convertStr,i) t2 = bit.band(num1,bit.rshift(0xff,2)) tempVar1=bit.bor(t2,bit.lshift(bit.band(t1,bit.rshift(0xff,6)),6)) tempVar2=bit.rshift(t1,2) elseif bit.band(num1,0xf0)== 0xe0 then local t1 = 0 local t2 = 0 local t3 = 0 t1 = bit.band(num1,bit.rshift(0xff,3)) i=i+1 num1=string.byte(convertStr,i) t2 = bit.band(num1,bit.rshift(0xff,2)) i=i+1 num1=string.byte(convertStr,i) t3 = bit.band(num1,bit.rshift(0xff,2)) tempVar1=bit.bor(bit.lshift(bit.band(t2,bit.rshift(0xff,6)),6),t3) tempVar2=bit.bor(bit.lshift(t1,4),bit.rshift(t2,2)) end resultStr[len] = string.char(tempVar1) .. string.char(tempVar2); len = len + 1; -- print(resultStr[len - 1]) i=i+1 num1=string.byte(convertStr,i) end return table.concat(resultStr, "", 1, len - 1) endend
0 0
- 纯lua实现 utf-16le 和 utf-8互转
- unicode的UTF-16BE和UTF-16LE
- [Charset]UTF-8, UTF-16, UTF-16LE, UTF-16BE的区别
- [Charset]UTF-8, UTF-16, UTF-16LE, UTF-16BE的区别
- [Charset]UTF-8, UTF-16, UTF-16LE, UTF-16BE的区别
- [Charset]UTF-8, UTF-16, UTF-16LE, UTF-16BE的区别
- [Charset]UTF-8, UTF-16, UTF-16LE, UTF-16BE的区别
- (转)[Charset]UTF-8, UTF-16, UTF-16LE, UTF-16BE的区别
- [Charset]UTF-8, UTF-16, UTF-16LE, UTF-16BE的区别
- UTF-8, UTF-16, UTF-16LE, UTF-16BE的区别
- ASCII、UTF-8、UTF-16BE、UTF-16LE编码方式的区别
- Unicode中的UTF-8, UTF-16, UTF-16LE, UTF-16BE编码及转换 | Java基础
- C++ UTF-8 转换助手 实现UTF-8 和UTF-16之间的转换
- unicode 和 utf-8 utf-16 utf-32 ASCII ANSI
- lua utf-8
- Unicode编码:UTF-8和UTF-16
- ASCII,Unicode和UTF-8,UTF-16
- UTF-8和UTF-16使用对比
- 1、文件测试_2、文件操作
- 求最大最小数
- 2016年搜主意原创作品征集
- IOS如何使用LaunchImage
- 未安装nginx输入ip直接访问工程
- 纯lua实现 utf-16le 和 utf-8互转
- GPU和CUDA介绍
- 十一的烦恼——我有资格说是程序员吗?
- java邮件发送通用类
- leetcode:Invert Binary Tree 【Java】
- Linux Shell特殊字符和控制字符大全
- 高德地图调用中出现的问题
- 1111
- C#循环使用File.Copy造成电脑死机