# Passing bytes from LUA to C

**URL:** <https://discuss.aerospike.com/t/passing-bytes-from-lua-to-c/2451>\
**Category:** User Defined Functions (UDF)\
**Created:** [February 2, 2016, 8:36am UTC](https://discuss.aerospike.com/t/passing-bytes-from-lua-to-c/2451 "2016-02-02T08:36:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![angelo.difino](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@angelo.difino](https://discuss.aerospike.com/u/angelo.difino)\
**Post date:** [February 2, 2016, 8:36am UTC](https://discuss.aerospike.com/t/passing-bytes-from-lua-to-c/2451/1 "2016-02-02T08:36:18Z")

</div>

Hi all,

I’m trying to pass some information (BLOB in aerospike aka byte in a java client aka bytes in LUA) from a LUA filtering routine to a C function.

Unfortunately from C side I’m not able to access any kind of information (lua\_isnumber, lua\_isstring and lua\_istable return false).

If I simply pass a byte (and NOT an array of byte) the information is retrieved correctly in the C routine.

Does somebody have any idea why this scenario is not working or does somebody have any pointer to the aerospike/lua documentation?

Thanks you soooo much!

My code is: From the Java Client I create a byte array of size = 6 and invoke a LUA routine called testingC in a prova2.lua file. I pass to LUA the byte

```
byte[] java_query_pool={13, 15, 11, 0, 99, 77};
rs =client.queryAggregate(null, stmt, "prova2", "testingC", Value.get(java_query_pool) );

```

in the LUA routine i can correctly retrieve the array of byte and I’m able to retrieve the size of the array and the contents of the array (i.e. I’m able to retrieve the 6-th data = 77)

```
local function myfilter1(pool_query)
  local fromlua=require("fromlua")     
  local sizee=bytes.size(pool_query)
  info (sizee)
  local unbyte=bytes.get_byte(pool_query,6)        
  info (unbyte)

```

If i pass the pool\_query (bytes) to the C routine the data is not available on C side (is even not recognized like strin/number/table). If i pass the unbyte (simple one byte) to C, the data is correctly retrieved

WORKING SCENARIO:

From LUA

```
local return_value = fromlua.match(unbyte); 

```

to the C routine

```
static int match(lua_State * L) 
{
  int rtrn = lua_tonumber(L, -1);

```

NOT WORKING SCENARIO

from LUA

`local return_value = fromlua.match(pool_query);`

to C

```
static int match(lua_State * L) {
  syslog(LOG_INFO, "whattisit");
  if (lua_isnumber(L, -1)) {
    syslog(LOG_INFO, "iz a number");
  } 
  else if (lua_isstring(L, -1)) {
    syslog(LOG_INFO, "iz a string");
  } 
  else if (lua_istable(L, -1)) {
    syslog(LOG_INFO, "iz a table");
  }
  else syslog(LOG_INFO, "iz NONE"); <-- this is printed out
  const void *rtrn=lua_topointer(L, -1); <-- return pointer to ZERO
  //int rtrn = lua_tonumber(L, -1); <-- return ZERO
  //const char *value = lua_tostring(L, -1); <-- return NULL

```

Any helps is more than appreciate! Thankssss

---

<div class="post-metadata">

**Author:** ![angelo.difino](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@angelo.difino](https://discuss.aerospike.com/u/angelo.difino)\
**Post date:** [February 2, 2016, 11:07am UTC](https://discuss.aerospike.com/t/passing-bytes-from-lua-to-c/2451/2 "2016-02-02T11:07:51Z")

</div>

Hello community

seems the bytes from LUA are userdata in C:

`lua_isuserdata(L, -1)`

I suppose I’ve to use

`const as_bytes *ud = lua_touserdata(L, -1);`

to retrieve the data

I keep update when i will got a successful scenario

---

<div class="post-metadata">

**Author:** ![angelo.difino](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@angelo.difino](https://discuss.aerospike.com/u/angelo.difino)\
**Post date:** [February 5, 2016, 8:15am UTC](https://discuss.aerospike.com/t/passing-bytes-from-lua-to-c/2451/3 "2016-02-05T08:15:55Z")

</div>

Hi all, I’m still having some issues trying to retrieve from the lua stack the array of byte (from c code).

Unfortunately if I try to

`as_bytes *ud = lua_touserdata(L, -1);`

the structure returned is not consistent (size 32562 instead of 7) and If i try to retrieve some data from it I receive a SIGSEGV.

I’ve been checked my code against the as\_lua example (that is working with no major issue).

Does anybody of you has a working example passing Bytes from lua to as\_byte in c? Thanksssss Angelo

PS: I’m wondering where can i find the aerospike source code that put on the stack the bytes structure so that i can track down what’s going on behind my source code

---

<div class="post-metadata">

**Author:** ![angelo.difino](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@angelo.difino](https://discuss.aerospike.com/u/angelo.difino)\
**Post date:** [February 5, 2016, 2:07pm UTC](https://discuss.aerospike.com/t/passing-bytes-from-lua-to-c/2451/4 "2016-02-05T14:07:19Z")

</div>

ok, i’ve figured out → again, my fault… i had to use

`as_bytes * ud=mod_lua_tobytes(L, -1);`

I leave the thread if anyone else will have the same problem
