简体   繁体   中英

LuaInterface: Call System.Encoding.UTF8.GetBytes from Lua Script

I am using LuaInterface to use .NET types in Lua. I wanted to showcase this ability by using the Cryptography classes from .NET. However, in order to pass a string to an RSACryptoProvider , I need to convert it to an array of bytes beforehand. This can normally be done using System.Encoding.UTF8.GetBytes . However, trying to call this function yields an error:

> bytes = luanet.System.Encoding.UTF8.GetBytes("foobar")
stdin:1: No such type: System.Encoding.UTF8.GetBytes
stack traceback:
        [C]: in function 'error'
        [string "local metatable = {}
                        ..."]:30: in function 'GetBytes'
        stdin:1: in main chunk
        [C]: ?

What is the proper way to call the GetBytes function? Am I missing something?

UTF8 is a static property of the class System. Text .Encoding.

Also, GetBytes is an instance method so call it with Lua's method call syntax.

So:

bytes = luanet.System.Text.Encoding.UTF8:GetBytes("foobar")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM