Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Encoding Lua,处理非ascii字节流,字节顺序更改_Encoding_Lua_Bytearray_Endianness_Decoding - Fatal编程技术网

Encoding Lua,处理非ascii字节流,字节顺序更改

Encoding Lua,处理非ascii字节流,字节顺序更改,encoding,lua,bytearray,endianness,decoding,Encoding,Lua,Bytearray,Endianness,Decoding,需要对字节流(可能包含非ascii字符)进行编码和解码,从uint16、uint32、uint64(其典型的C/C++含义)到uint16、uint32、uint64(其典型的C/C++含义),并注意尾端性。在Lua中,什么是一种高效且有望实现跨平台的方法 我的目标arch是64位x86_64,但希望保持它的可移植性(如果在性能方面不花费我的话) e、 g 解码(假设当前处于Lua字符串中)-- 0x00、0x1d、0xff、0x23、0x44、0x32(小端) 作为- uint16:(0x1d

需要对字节流(可能包含非ascii字符)进行编码和解码,从uint16、uint32、uint64(其典型的C/C++含义)到uint16、uint32、uint64(其典型的C/C++含义),并注意尾端性。在Lua中,什么是一种高效且有望实现跨平台的方法

我的目标arch是64位x86_64,但希望保持它的可移植性(如果在性能方面不花费我的话)

e、 g

解码(假设当前处于Lua字符串中)-- 0x00、0x1d、0xff、0x23、0x44、0x32(小端) 作为- uint16:(0x1d00)=7424 uint32:(0x324423ff)=843326463


如果有人能举例说明,那就太好了。

关于从字节到int的转换(注意字节级别的尾数和符号):

当我们在另一个方向时:

function int_to_bytes(num,endian,signed)
    if num<0 and not signed then num=-num print"warning, dropping sign from number converting to unsigned" end
    local res={}
    local n = math.ceil(select(2,math.frexp(num))/8) -- number of bytes to be used.
    if signed and num < 0 then
        num = num + 2^n
    end
    for k=n,1,-1 do -- 256 = 2^8 bits per char.
        local mul=2^(8*(k-1))
        res[k]=math.floor(num/mul)
        num=num-res[k]*mul
    end
    assert(num==0)
    if endian == "big" then
        local t={}
        for k=1,n do
            t[k]=res[n-k+1]
        end
        res=t
    end
    return string.char(unpack(res))
end
function int_to_字节(num、endian、signed)
如果num请查看和库

在本例中,我使用
struct.unpack
将Lua字符串解码为两个整数,并使用强制小端编码:

require 'struct'
-- convert character codes to a Lua string - this may come from your source
local str = string.char(0x00, 0x1d, 0xff, 0x23, 0x44, 0x32)
-- format string: < = little endian, In = unsigned int (n bytes)
local u16, u32 = struct.unpack('<I2I4', str)
print(u16, u32) --> 7424    843326463
需要“struct”
--将字符代码转换为Lua字符串-这可能来自您的源代码
本地str=string.char(0x00、0x1d、0xff、0x23、0x44、0x32)
--格式字符串:<=little-endian,In=unsigned int(n字节)
local u16,u32=struct.unpack(“我对不检查参数的“Int16ToByte”函数的建议:

function Int16ToBytes(num, endian)
  if num < 0 then 
      num = num & 0xFFFF
  end

  highByte = (num & 0xFF00) >> 8
  lowByte  = num & 0xFF

  if endian == "little" then
      lowByte, highByte = highByte, lowByte
  end

  return string.char(highByte,lowByte)
end
函数Int16ToBytes(num,endian) 如果num<0,则 num=num&0xFFFF 结束 高字节=(num&0xFF00)>>8 lowByte=num&0xFF 如果endian==“little”,则 低字节,高字节=高字节,低字节 结束 返回字符串.char(高字节,低字节) 结束
非常有指导意义。我似乎通过您的示例学习了大量Lua,说真的。在函数字节中,行n=(n>2^(#t-1)-1)和(n-2^#t)或者n我认为应该是#t*8,或者#t。是你想要的位数,而不是字节数。似乎你是对的!谢谢你的建议,我已经在我的答案中更正了。@jpjacobs-非常感谢。处理有符号数时,函数int~u bytes也需要更正:如果有符号且num<0,那么下一行2^n应该是num=num+2^(8*n)int_to_字节并不总是正确处理有符号数字(当frexp的exp是8的倍数时)。例如,-32769应该是0xFF 0xFF 0x80 0x00,但函数本身返回0x7F 0xFF。看起来非常简单和优雅,但是我似乎没有“struct”扩展模块。Luarock生成/安装它时出错。将尝试解决此问题并尝试此操作。谢谢!@michal kottman,在修复Luarock并安装后尝试了此代码truct'但是对于lua来说,解包的第二个参数(即str)不是字符串。为了调试,我尝试了这个小代码(它没有解包,但似乎也不能像预期的那样工作--
>str=string.char(0x00,0xff)
>local u16=struct.unpack('@michal kottman,抱歉!修复了它。lua 5.1中需要轻微的更改(至少在我的系统上)。只需做一个:
struct=require(“struct”)
奇怪的是,我测试了Lua5.1中的代码,它工作正常(可能我有一个较旧版本的
struct
),但我很高兴它现在能为您工作,它是一个非常方便的二进制数据工具。。。
function Int16ToBytes(num, endian)
  if num < 0 then 
      num = num & 0xFFFF
  end

  highByte = (num & 0xFF00) >> 8
  lowByte  = num & 0xFF

  if endian == "little" then
      lowByte, highByte = highByte, lowByte
  end

  return string.char(highByte,lowByte)
end