超过一个字节后的lua解析 [英] lua parsing after more than one byte

查看:66
本文介绍了超过一个字节后的lua解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在超过一个字节后解析二进制文件?例如:

how can I parse an binary after more than one byte? for example:

56 30 30 31 07 00 00 00 EF BF BD EF BF BD 2A 5C EF BF BD 03 01 45 7E 12 02 EF BF BD 00 EF BF BD 1F 56 30 30 31 01 00 00 00 EF BF BD EF BF BD BD2A 5C EF BF BD 03 02 45 24 56 30 30 31 04 00 00 00 EF BF BD EF BF BD 2A 5C EF BF BD 03 02 45 13 00 00 00 56 30 30 31 07 00 00 00 EF BF BD EF BF BD 2A5C EF BF BD 03 01 45 7E 12 02 24 00 4D EF BF

56 30 30 31 07 00 00 00 EF BF BD EF BF BD 2A 5C EF BF BD 03 01 45 7E 12 02 EF BF BD 00 EF BF BD 1F 56 30 30 31 01 00 00 00 EF BF BD EF BF BD 2A 5C EF BF BD 03 02 45 24 56 30 30 31 04 00 00 00 EF BF BD EF BF BD 2A 5C EF BF BD 03 02 45 13 00 00 00 56 30 30 31 07 00 00 00 EF BF BD EF BF BD 2A 5C EF BF BD 03 01 45 7E 12 02 24 00 4D EF BF

我想在0x56 0x30 0x30 0x31之后进行解析.我怎样才能做到这一点?在每个新的0x56 0x30 0x30 0x31之前,旧数据包(字符串)应结束.

I want to parse this after 0x56 0x30 0x30 0x31. How can I do this? Before every new 0x56 0x30 0x30 0x31 the old packet(string) should end.

像这样:

56 30 30 31 07 00 00 00 EF BF BD EF BF BD 2A 5C EF BF BD 03 01 45 7E 12 02 EF BF BD 00 EF BF BD 1F

56 30 30 31 07 00 00 00 EF BF BD EF BF BD 2A 5C EF BF BD 03 01 45 7E 12 02 EF BF BD 00 EF BF BD 1F

56 30 30 31 01 00 00 00 EF BF BD EF BF BD 2A 5C EF BF BD 03 02 45 24

56 30 30 31 01 00 00 00 EF BF BD EF BF BD 2A 5C EF BF BD 03 02 45 24

56 30 30 31 04 00 00 00 EF BF BD EF BF BD 2A 5C EF BF BD 03 02 45 13 00 00 00

56 30 30 31 04 00 00 00 EF BF BD EF BF BD 2A 5C EF BF BD 03 02 45 13 00 00 00

56 30 30 31 07 00 00 00 EF BF BD EF BF BD 2A 5C EF BF BD 03 01 45 7E 12 02 24 00 4D EF BF

56 30 30 31 07 00 00 00 EF BF BD EF BF BD 2A 5C EF BF BD 03 01 45 7E 12 02 24 00 4D EF BF

我已经做了一些类似的事情,将一个字节的数据解析到一个表中.但是我无法将其更改为我的新问题.多数民众赞成在一个字节0x7E后进行解析的代码.

I already did something simular for parsing after one byte into an table. But I can not change it into my new problem. Thats the code for parsing after one Byte 0x7E.

function print_table(tab)
print("Table:") 
for key, value in pairs(tab) do
  io.write(string.format("%02X ", value))  
end
print("\n") 
end

local function read_file(path, callback) 
local file = io.open(path, "rb") 
if not file then 
 return nil
end
local t = {} 
repeat
local str = file:read(4 * 1024)   
for c in (str or ''):gmatch('.') do  
    if c:byte() == 0x7E then 
        callback(t) -- function print_table
        t = {}
    else
        table.insert(t, c:byte())  
    end
end
until not str
file:close() 
return t 
end

local result = {}
function add_to_table_of_tables(t)
table.insert(result, t) 
end

local fileContent = read_file("file.dat", print_table)

首先将56 30 30 31写入字符串很重要.谢谢您的帮助!

It is important that 56 30 30 31 is as first written in the string. Thank your for your help!

我也需要从文件中读取输入内容.我正在这样读取文件:

I need it also with reading my input in from an file. I am reading my file like this in:

local function read_file(path) --function read_file
  local file = io.open(path, "rb") -- r read mode and b binary mode
  if not file then return nil end
  local content = file:read "*all" -- *all reads the whole file
  file:close()
  return content
end

推荐答案

您可以使用 gsub 将目标子字符串替换为输入字符串唯一的单个字符,在此示例中,我将使用 \ n .之后,您可以使用 gmatch 在其中选择一个不是替代字符的字符.

You can use a gsub to replace the target substring with a single char unique to the input string, I will use \n for this example. after that you can use gmatch where it selects a run of char that are not the substituted char.

local input = [[56 30 30 31 07 00 00 00 EF BF BD EF BF BD 2A 5C EF BF BD 03 01 45 7E 12 02 EF BF BD 00 EF BF BD 1F 56 30 30 31 01 00 00 00 EF BF BD EF BF BD 2A 5C EF BF BD 03 02 45 24 56 30 30 31 04 00 00 00 EF BF BD EF BF BD 2A 5C EF BF BD 03 02 45 13 00 00 00 56 30 30 31 07 00 00 00 EF BF BD EF BF BD 2A 5C EF BF BD 03 01 45 7E 12 02 24 00 4D EF BF]]

local pattern ="([^\n]+)"
local rowPrefix = "56 30 30 31"

input = input:gsub(rowPrefix, "\n")

for row in input:gmatch(pattern) do
  print(rowPrefix .. row)
end

输出:

56 30 30 31 07 00 00 00 EF BF BD EF BF BD 2A 5C EF BF BD 03 01 45 7E 12 02 EF BF BD 00 EF BF BD 1F 
56 30 30 31 01 00 00 00 EF BF BD EF BF BD 2A 5C EF BF BD 03 02 45 24 
56 30 30 31 04 00 00 00 EF BF BD EF BF BD 2A 5C EF BF BD 03 02 45 13 00 00 00 
56 30 30 31 07 00 00 00 EF BF BD EF BF BD 2A 5C EF BF BD 03 01 45 7E 12 02 24 00 4D EF BF


更多信息的资源:


Resources for more information:

在Lua中编程:20.1 –模式匹配功能

Lua 5.3参考手册:string.gmatch

Lua 5.3参考手册:string.gsub

这篇关于超过一个字节后的lua解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆