如何将cdata结构转换为lua字符串?

如何将cdata结构转换为lua字符串?,第1张

概述我正在编写一个需要在LuaJit中读取一些复杂二进制消息的小应用程序. 我一直在使用bit模块和string.rep.然而,这一切都非常麻烦.我是使用LuaJit的新手,并认为使用FFI可能会有更简单的方法. 在C中我可以声明这样的结构: struct mystruct{ uint32_t field1; char field2[6]; uin64_t field3; 我正在编写一个需要在LuaJit中读取一些复杂二进制消息的小应用程序.

我一直在使用bit模块和string.rep.然而,这一切都非常麻烦.我是使用LuaJit的新手,并认为使用FFI可能会有更简单的方法.

在C中我可以声明这样的结构:

struct mystruct{  uint32_t fIEld1;  char     fIEld2[6];  uin64_t  fIEld3;  short    fIEld4;} __attribute__(packed);

在阅读LuaJit的FFI时,你似乎可以宣布

ffi.cdef[[    #pragma pack(1)    struct mystruct    {      uint32_t fIEld1;      char     fIEld2[6];      uin64_t  fIEld3;      short    fIEld4;    };]]

然后我可以创建一个mystruct并访问这样的字段:

local ms = ffi.new("mystruct")ms.fIEld1 = 32;// ... etc

但是,如何将其转换为lua字符串

我尝试了这个,但它似乎没有做我想要的.

local s = tostring(ms)

还有这个:

local s = ffi.string(ms)

产生以下错误“错误的参数#1到’字符串'(不能将’struct mystruct’转换为’const char *’)”

所以我试了一个演员:

local s = ffi.string(ffi.cast("char*",ms))

没有错误,但它在电线上看起来不对.

解决方法 将 ffi.string与非类似字符串的参数一起使用时,必须明确指定长度:

str = ffi.string(ptr [,len])

Creates an interned Lua string from the data pointed to by ptr.

If the optional argument len is missing,ptr is converted to a “char *” and the data is assumed to be zero-terminated. The length of the string is computed with strlen().

运行以下代码时,我得到了预期的(小端)结果:

ffi = require 'ffi'ffi.cdef[[    typedef unsigned long uint32_t;    typedef unsigned long long uint64_t;    #pragma pack(1)    struct mystruct    {      uint32_t fIEld1;      char     fIEld2[6];      uint64_t  fIEld3;      short    fIEld4;    };]]function string.tohex(str)    return (str:gsub('.',function (c)        return string.format('%02X',string.byte(c))    end))endms = ffi.new('struct mystruct',1,{2,3,4,5,6,7},8,9)s = ffi.string(ms,ffi.sizeof(ms)) -- specify how long the byte sequence isprint(s:tohex()) --> 01000000020304050607    

str = ffi.string(ptr [,len])

Creates an interned Lua string from the data pointed to by ptr.

If the optional argument len is missing,ptr is converted to a "char *" and the data is assumed to be zero-terminated. The length of the string is computed with strlen().

000000000000900

更新:我知道这不是原始问题的一部分,但我刚刚学会了这个技巧,为了完成,这里有一种方法将Lua字符串转换回FFI cdata:

data = ffi.new('struct mystruct')   -- create a new cdata    ffi.copy(data,s,ffi.sizeof(data)) -- fill it with data from Lua string 's'    print(data.fIEld1,data.fIEld4)     --> 1   9
总结

以上是内存溢出为你收集整理的如何将cdata结构转换为lua字符串?全部内容,希望文章能够帮你解决如何将cdata结构转换为lua字符串?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1231356.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存