vbscript:将 unicode 字符串转换为字节数组 [英] vbscript: convert unicode string to array of bytes

查看:42
本文介绍了vbscript:将 unicode 字符串转换为字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 unicode 字符串传递给 vbscript 过程(不是visual basic 6,而是vbscript).我想逐个字符迭代 unicode 字符串,获取每个符号的代码,将代码截断到字节范围 [0..255] 并创建字节数组.

I have unicode string passed to vbscript procedure (not visual basic 6, but vbscript). I want to iterate unicode string char by char, get code for every symbol, truncate code to byte range [0..255] and create array of bytes.

这样,与原始 unicode 字符串相比,新的字节数组在内存中应该小两倍.我将通过 ADODB.Stream 对象将此数组保存到文件中进一步

This way new array of bytes should be twice smaller in memory compared to original unicode string. I am going save this array to file via ADODB.Stream object further

如何将 unicode 字符串转换为字节数组,并将符号代码截断为字节范围?

How can I convert unicode string to bytes array with symbol code truncated to byte range?

先谢谢你!

推荐答案

在 vbs 中似乎没有办法创建字节数组(尽管它在 Visual Basic 中非常简单)——所有数组都是变体数组.

It seems there is no way to create array of bytes in vbs (though it's very straightforward in visual basic) -- all arrays are arrays of variants.

任务是通过字符串类型将二进制流从服务器发送到 vbs 脚本.我通过在服务器上使用 CDATA 部分创建 Xml 文档找到了解决方案,该部分包含作为字符串数据的 base64 编码字节数组.

The task was to send binary stream from server to vbs script via string type. I have found the solution by creating Xml Document on the server with CDATA section that contains base64 coded array of bytes as string data.

客户端 (vbs) 执行以下操作:

Client (vbs) do the following:

set xmlDoc = CreateObject("Microsoft.XmlDom")
xmlDoc.loadXML(dataFromServer)
base64str = xmlDoc.DocumentElement.Text  ' it's base64 coded binary stream
arrayOfBytes = decodeBase64(base64str)

Function decodeBase64(base64)  
  set dm = CreateObject("Microsoft.XMLDOM")
  set el = dm.createElement("tmp")
  el.DataType = "bin.base64"
  el.Text = base64
  decodeBase64 = el.NodeTypedValue
  set dm = Nothing
End Function

这篇关于vbscript:将 unicode 字符串转换为字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆