lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Tuesday 14, Digital wrote:
> I've been trying this for a very long time, never succeeded in passing
> binary data.
> 
> But here is a function:
> 
> static int testfunc(lua_State *L)
> {
> printf("test func\n");
> 
> char *arg1;
> int i;
> size_t len = 0;
> arg1 = luaL_checklstring(L, 1, &len);
> //arg1 = luaL_checkudata(L, 1, &len);
> //arg1 = lua_touserdata(L, 1);
> //len=strlen(arg1);
> printf("Size = %s\n",arg1);

Don't try to print binary data with printf().

printf("arg1 len = %zd\n", len);
/* print hex dump of arg1 */
for(i = 0; i < len; i++) {
  if((i % 16) == 0 && i > 0) printf("\n");
  printf("%02x ", arg1[i]);
}
printf("\n");

> 
> FILE *sfile;
> sfile = fopen("testfilebinary.txt","wb");
> fwrite(arg1,len,1,sfile);

Also check the return value from fwrite().  Also I would swap the 'len' & '1' 
parameters.

size_t rc = fwrite(arg1, 1, len, sfile);
printf("fwrite() = %zd\n", rc);

> fclose(sfile);
> 
> return 0;
> }


-- 
Robert G. Jakabosky