gpt4 book ai didi

arrays - 将二进制文件读入数组

转载 作者:行者123 更新时间:2023-12-01 15:16:06 25 4
gpt4 key购买 nike

我有一个由一系列 32 位带符号整数值(小端)组成的文件。我如何将其读入数组(或类似的)数据结构?

我试过这个:

block = 4
while true do
local int = image:read(block)
if not int then break end
memory[i] = int
i = i + 1
end

但是内存表不包含与文件中匹配的值。如有任何建议,我们将不胜感激。

最佳答案

这个小示例将从文件中读取一个 32 位带符号整数并打印它的值。

    -- convert bytes (little endian) to a 32-bit two's complement integer    function bytes_to_int(b1, b2, b3, b4)      if not b4 then error("need four bytes to convert to int",2) end      local n = b1 + b2*256 + b3*65536 + b4*16777216      n = (n > 2147483647) and (n - 4294967296) or n      return n    end    local f=io.open("test.bin") -- contains 01:02:03:04    if f then        local x = bytes_to_int(f:read(4):byte(1,4))        print(x) --> 67305985    end

关于arrays - 将二进制文件读入数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4259770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com