美文网首页
ToLua的Example示例学习笔记16_Int64

ToLua的Example示例学习笔记16_Int64

作者: 凌枫望星月 | 来源:发表于2020-05-13 14:26 被阅读0次

展示了ToLua新增Int64类的使用方法。

1」代码

操作代码如下:

            function TestInt64(x)                
                x = 789 + x
                assert(tostring(x) == '9223372036854775807')                                               
                local low, high = int64.tonum2(x)                
                print('x value is: '..tostring(x)..' low is: '.. low .. ' high is: '..high.. ' type is: '.. tolua.typename(x))           
                local y = int64.new(1,2)                
                local z = int64.new(1,2)
                
                if y == z then
                    print('int64 equals is ok, value: '..int64.tostring(y))
                end

                x = int64.new(123)                   
            
                if int64.equals(x, 123) then
                    print('int64 equals to number ok')
                else
                    print('int64 equals to number failed')
                end

                x = int64.new('78962871035984074')
                print('int64 is: '..tostring(x))

                local str = tostring(int64.new(3605690779, 30459971))                
                local n2 = int64.new(str)
                local l, h = int64.tonum2(n2)                        
                print(str..':'..tostring(n2)..' low:'..l..' high:'..h)                  

                print('----------------------------uint64-----------------------------')
                x = uint64.new('18446744073709551615')                                
                print('uint64 max is: '..tostring(x))
                l, h = uint64.tonum2(x)      
                str = tostring(uint64.new(l, h))
                print(str..':'..tostring(x)..' low:'..l..' high:'..h)     

                return y
            end

c#代码如下:

        new LuaResLoader();
        LuaState lua = new LuaState();
        lua.Start();
        lua.DoString(script, "TestInt64.cs");                

        LuaFunction func = lua.GetFunction("TestInt64");
        func.BeginPCall();
        func.Push(9223372036854775807 - 789);
        func.PCall();
        long n64 = func.CheckLong();        
        Debugger.Log("int64 return from lua is: {0}", n64);
        func.EndPCall();
        func.Dispose();
        func = null;

        lua.CheckTop();
        lua.Dispose();
        lua = null;             

2」需要了解的部分

  • 在Lua代码中,以local low, high = int64.tonum2(x) 的方式来获取高位和低位。
  • 在Lua中:x = integrate4.new(123)y = integrate4.new('12345678987654321') 创建的值与括号里的值一样。可以用 int64.equals(x, 123)验证, 也可以用 == 来比较两个很大的Int64数。
  • local y = int64.new(1,2)的值很大,可以用tostring(y)表示。

相关文章

网友评论

      本文标题:ToLua的Example示例学习笔记16_Int64

      本文链接:https://www.haomeiwen.com/subject/tyynnhtx.html