Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8520867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:39:08+00:00 2026-06-11T06:39:08+00:00

I’m in the middle of writing a small application that needs to read some

  • 0

I’m in the middle of writing a small application that needs to read some complex binary messages in LuaJit.

I’ve been using the bit module and string.rep a lot. However, it’s all very cumbersome. I’m new to using LuaJit and think there might be a much easier way using FFI.

In C I can declare a structure like this:

struct mystruct
{
  uint32_t field1;
  char     field2[6];
  uin64_t  field3;
  short    field4;
} __attribute__(packed);

In reading LuaJit’s FFI it seems you can declare

ffi.cdef[[
    #pragma pack(1)
    struct mystruct
    {
      uint32_t field1;
      char     field2[6];
      uin64_t  field3;
      short    field4;
    };
]]

I can then create a mystruct and access the fields like this:

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

But, how do I convert this back into a lua string?

I tried this, but it didn’t seem to do what I wanted.

local s = tostring(ms)

and this:

local s = ffi.string(ms)

produces the following error “bad argument #1 to ‘string’ (cannot convert ‘struct mystruct’ to ‘const char *’)”

So I tried a cast:

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

No error, but it looks wrong on the wire.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T06:39:10+00:00Added an answer on June 11, 2026 at 6:39 am

    You have to explicitly specify the length when using ffi.string with a non-string-like parameter:

    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().

    When running the following code, I get the expected (little endian) result:

    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))
    end
    
    ms = 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 is
    print(s:tohex()) --> 0100000002030405060708000000000000000900
    

    Update: I know this is not a part of the original question, but I just learned this trick, and in order to be complete, here is a way to convert Lua string back to 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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.