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 8184537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:32:21+00:00 2026-06-07T01:32:21+00:00

I hava a class like: class SomeClass { void initFromBuffer(void* buffer,int length); void initFromString(const

  • 0

I hava a class like:

class SomeClass
{
   void initFromBuffer(void* buffer,int length);
   void initFromString(const std::string& str);
}

Using tolua++, got the binding like:

static int SomeClass_initFromBuffer00(lua_State* tolua_S)
{
  SomeClass* self = (SomeClass*)  tolua_tousertype(tolua_S,1,0);
  void* buffer = ((void*)  tolua_touserdata(tolua_S,2,0));
  int length = ((int)  tolua_tonumber(tolua_S,3,0));
  self->initFromBuffer(buffer,length);
}

and:

static int SomeClass_initFromString00(lua_State* tolua_S)
{

   SomeClass* self = (SomeClass*)  tolua_tousertype(tolua_S,1,0);
   const std::string str = ((const std::string)  tolua_tocppstring(tolua_S,2,0));
   self->initFromString(str);
   tolua_pushcppstring(tolua_S,(const char*)str);
}

Now,i want to pass binary data from lua to c++,the binary has ‘\0’ in it,so if i use initFromString to pass it, the binary data will be trimed. But if i use initFromBuffer to pass it, i got bad ptr at `void* buffer = ((void*) tolua_touserdata(tolua_S,2,0));, the pointer is null.

So, how could i pass binary string from lua to c++?

  • 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-07T01:32:22+00:00Added an answer on June 7, 2026 at 1:32 am

    Maybe you should stop using Tolua’s bad APIs and use plain Lua’s actually good APIs. Both std::string and Lua strings are capable of storing embedded null characters. The only reason tolua_tocppstring causes truncation is because the function name is a lie. It doesn’t convert it to a C++ string; it converts it to a C string, a const char*.

    The correct answer is to use the proper API function:

    std::string fromLuaStack(lua_State *lua, int stackIx)
    {
      size_t len;
      const char *str = lua_tolstring(lua, stackIx, &len);
      return std::string(str, len);
    }
    

    Similarly, you can use lua_pushlstring to push a std::string onto the stack.

    It’s unfortunate that Tolua doesn’t have better documentation, as there may be a function to do this all directly. If there is, I couldn’t find it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I hava some code like this: class LooperThread extends Thread { public Handler mHandler;
I hava a jar with a main file like this: public class StartMesekocka3D {
I hava a class called DBhelper, and now I created a new class called
I hava a binary file like this : offset | size/type | Description -------+-----------+-----------------------------------------------------------
i hava a c++ method(for java,jni) like follow,when i repeat call this from java
i hava a class that hold a list for each request but each request
I hava an ArrayList of String values (they need to be formatted as such)
I hava a perl subroutine where i would like to pass parameters as a
I hava a generic class called ArrayBag. I want to override equals method. So
My issue is quite simple, I hava a generic parent class with the following

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.