lua中table和string的相互转化

--[[
	Table转化为string 
--]]
function TableToStr(t, bArry)
    if t == nil then
        return ""
    end
    local retstr = "{"

    local i = 1
    for key, value in pairs(t) do
        local signal = ","
        if i == 1 then
            signal = ""
        end

        if bArry then
            retstr = retstr .. signal .. ToStringEx(value)
        else
            if type(key) == "number" or type(key) == "string" then
                retstr = retstr .. signal .. "[" .. ToStringEx(key) .. "]=" .. ToStringEx(value)
            else
                if type(key) == "userdata" then
                    retstr =
                        retstr .. signal .. "*s" .. TableToStr(getmetatable(key)) .. "*e" .. "=" .. ToStringEx(value)
                else
                    retstr = retstr .. signal .. key .. "=" .. ToStringEx(value)
                end
            end
        end
        i = i + 1
    end
    retstr = retstr .. "}"
    return retstr 
end

--[[
	String转画table
--]]
function StrToTable(str)
    if str == nil or type(str) ~= "string" or str == "" then
        return {}
    end
    return loadstring("return " .. str)()
end

--[[
	table与字符串转化	
--]]
function ToStringEx(value)
    if type(value) == "table" then
        return TableToStr(value)
    elseif type(value) == "string" then
        return '"' .. value .. '"'
    else
        return tostring(value)
    end
end

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是用C代码实现Lua table与byte[]的相互换的示例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include "lua.h" #include "lauxlib.h" #include "lualib.h" // 将字节数组换为Lua table static int byte_array_to_lua_table(lua_State* L) { size_t len; const char* str = luaL_checklstring(L, 1, &len); lua_newtable(L); for (int i = 0; i < len; i++) { lua_pushinteger(L, i + 1); lua_pushinteger(L, (int)str[i]); lua_settable(L, -3); } return 1; } // 将Lua table换为字节数组 static int lua_table_to_byte_array(lua_State* L) { luaL_checktype(L, 1, LUA_TTABLE); size_t len = lua_rawlen(L, 1); char* str = (char*)malloc(len); for (int i = 0; i < len; i++) { lua_pushinteger(L, i + 1); lua_gettable(L, 1); int val = luaL_checkinteger(L, -1); str[i] = (char)val; lua_pop(L, 1); } lua_pushlstring(L, str, len); free(str); return 1; } int main() { lua_State* L = luaL_newstate(); luaL_openlibs(L); lua_pushcfunction(L, byte_array_to_lua_table); lua_setglobal(L, "byte_array_to_lua_table"); lua_pushcfunction(L, lua_table_to_byte_array); lua_setglobal(L, "lua_table_to_byte_array"); // 测试byte_array_to_lua_table函数 const char* str = "hello world"; size_t len = strlen(str); lua_getglobal(L, "byte_array_to_lua_table"); lua_pushlstring(L, str, len); lua_call(L, 1, 1); luaL_checktype(L, -1, LUA_TTABLE); printf("byte_array_to_lua_table result:\n"); for (int i = 0; i < len; i++) { lua_pushinteger(L, i + 1); lua_gettable(L, -2); int val = luaL_checkinteger(L, -1); printf("%d ", val); lua_pop(L, 1); } printf("\n"); // 测试lua_table_to_byte_array函数 lua_newtable(L); for (int i = 0; i < len; i++) { lua_pushinteger(L, i + 1); lua_pushinteger(L, (int)str[i]); lua_settable(L, -3); } lua_getglobal(L, "lua_table_to_byte_array"); lua_pushvalue(L, -2); lua_call(L, 1, 1); size_t size; const char* res = luaL_checklstring(L, -1, &size); printf("lua_table_to_byte_array result:\n"); for (int i = 0; i < size; i++) { printf("%c", res[i]); } printf("\n"); lua_close(L); return 0; } ``` 上述代码,我们通过 `byte_array_to_lua_table` 函数将字节数组换为 Lua table;通过 `lua_table_to_byte_array` 函数将 Lua table 换为字节数组。在测试时,我们先使用 `byte_array_to_lua_table` 函数将字节数组换为 Lua table,再使用 `lua_table_to_byte_array` 函数将 Lua table 换为字节数组,最后比较换前后的字节数组是否一致。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值