System.Text.UTF8Encoding From VBScript

2019-08-31 22:44发布

How would do you call GetBytes (of System.Text.UTF8Encoding) from VBScript?

You can use the .NET Framework from VBScript as its exposed to COM, but I haven't come across good documentation/manuals.

1条回答
淡お忘
2楼-- · 2019-08-31 23:28

If you are looking for equivalent in VBScript, you can do it using Ado Stream :

Const adTypeBinary = 1
Dim adoStr, bytesthroughado
Set adoStr = CreateObject("Adodb.Stream")
    adoStr.Charset = "utf-8"
    adoStr.Open
    adoStr.WriteText "你好Ğ"
    adoStr.Position = 0 'reset position
    adoStr.Type = adTypeBinary
    adoStr.Position = 3 'skip bom
    bytesthroughado = adoStr.Read 'get bytes
    WScript.Echo LenB(bytesthroughado) 'length
    adoStr.Close
Set adoStr = Nothing

It's possible to access some .Net components (from mscorlib) from VBScript.

Dim encoding, bytesthroughdotnet
Set encoding = CreateObject("System.Text.UTF8Encoding")
    bytesthroughdotnet = encoding.GetBytes_4("你好Ğ") 'get bytes
    WScript.Echo LenB(bytesthroughdotnet) 'length
Set encoding = Nothing
查看更多
登录 后发表回答