


-- 2023.6.29
-- 使用算数运算代替位运算





module = {}

function module:print_t ( t )  
    local print_r_cache={}
    local function sub_print_r(t,indent)
        if (print_r_cache[tostring(t)]) then
            print(indent.."*"..tostring(t))
        else
            print_r_cache[tostring(t)]=true
            if (type(t)=="table") then
                for pos,val in pairs(t) do
                    if (type(val)=="table") then
                        print(indent.."["..pos.."] => "..tostring(t).." {")
                        sub_print_r(val,indent..string.rep(" ",string.len(pos)+8))
                        print(indent..string.rep(" ",string.len(pos)+6).."}")
                    elseif (type(val)=="string") then
                        print(indent.."["..pos..'] => "'..val..'"')
                    else
                        print(indent.."["..pos.."] => "..tostring(val))
                    end
                end
            else
                print(indent..tostring(t))
            end
        end
    end
    if (type(t)=="table") then
        print(tostring(t).." {")
        sub_print_r(t,"  ")
        print("}")
    else
        sub_print_r(t,"  ")
    end
    print()
end




function byte_to_hex(byte,size)
    local str_table="0123456789ABCDEF"
    local str_ret=""
    if(size==2) then
        str_ret=string.char(
            -- 右移12位，//4096
            string.byte( str_table,(math.floor(byte/4096)%16)+1),
            string.byte( str_table,(math.floor(byte/256)%16)+1),
            string.byte( str_table,(math.floor(byte/16)%16)+1),
            string.byte(str_table,(byte%16)+1))
    else
        str_ret=string.char(string.byte(str_table,math.floor((byte%256)/16)+1),string.byte(str_table,(byte%256)%16+1))
    end
    return str_ret
end

-- function byte_to_num(byte)
--     local str_ret=""
--     while true do


-- 打印数组
function module:print_a ( a,size ,fmt)  
    local str=""
    if(a==nil) then return end
    if(size==nil) then size=1 end
    for pos,val in pairs(a) do
        str=str..byte_to_hex(val,size)..", "
    end
    print(str)
end



function module:__tostring()
    return "print table package"
 end





return module
