在 VBScript 中对字符串进行 Base64 编码
我有一个 Web 服务加载驱动程序,它是 Windows 脚本文件 (WSF),其中包括一些 VBScript 和 JavaScript 文件。 我的 Web 服务要求传入消息采用 Base64 编码。 我目前有一个 VBScript 函数可以执行此操作,但效率非常低(内存密集型,主要是由于 VBScript 糟糕的字符串连接)
[旁白; 是的,我看过Jeff 的最新博文。 连接发生在大小为 1,000 到 10,000 字节的消息之间的循环中。]
我尝试过使用一些自定义字符串连接例程; 一种使用数组,一种使用 ADODB.Stream。 这些有一点帮助,但我认为如果我有其他方法来编码消息而不是通过我自己的 VBS 函数,会更有帮助。
是否有其他方法对我的消息进行编码,最好使用本机 Windows 方法?
I have a web service load driver that's a Windows Script File (WSF), that includes some VBScript and JavaScript files. My web service requires that the incoming message is base64 encoded. I currently have a VBScript function that does this, but it's very inefficient (memory intensive, mostly due to VBScripts awful string concatenation)
[Aside; Yes, I've seen Jeff's latest blog post. The concatenation is happening in a loop across messages that are 1,000's to 10,000's bytes in size.]
I've tried using some custom string concatenation routines; one using an array and one using ADODB.Stream. These help, a little, but I think it would help more if I had some other way of encoding the message rather than via my own VBS function.
Is there some other way of encoding my message, preferebly using native Windows methods?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我最初使用的是 Antonin Foller 的一些 VBScript 代码:
Base64 编码 VBS 函数 和 Base64解码VBS函数。
搜索 Antonin 的网站,我看到他有一些 引用的可打印编码的代码,使用 CDO.Message对象,所以我尝试了。
最后,我将 Mark 的回答中提到的代码移植到 VBScript (还使用了 这个 SO问题),并使用了Stream___StringToBinary和Stream_BinaryToString 函数用于获取使用 MSXML 编码的函数。
我运行了一个快速测试,测量所有四种方法中 1,500 个字符消息的编码时间(我需要发送到 Web 服务的平均消息大小):
以下是结果:
在测试运行时,我还监视了内存利用率(Windows 任务管理器中 cscript.exe 进程的内存使用情况) 。 我没有任何原始数据,但引用的可打印解决方案和 MSXML 解决方案的内存利用率均低于 VBScript 解决方案(前者为 7,000K,VBScript 约为 16,000K)。
我决定为我的驱动程序采用 MSXML 解决方案。 对于那些感兴趣的人,这是我正在使用的代码:
I was originally using some VBScript code from Antonin Foller:
Base64 Encode VBS Function and Base64 Decode VBS Function.
Searching Antonin's site, I saw he had some code for quoted printable encoding, using the CDO.Message object, so I tried that.
Finally, I ported the code mentioned in Mark's answer to VBScript (also used some code from this SO question), and used the Stream___StringToBinary and Stream_BinaryToString functions from Antonin's site to get functions that used MSXML encoding.
I ran a quick test to measure the encoding time for a 1,500 character message (the average message size I need to send to my web service) across all four methods:
Here are the results:
I also monitored the memory utilization (Mem Usage for the cscript.exe process in the Windows Task Manager) while the test was running. I don't have any raw numbers, but the memory utilization for both the quoted printable and MSXML solutions were below the VBScript solution (7,000K for the former, around 16,000K for VBScript).
I decided to go with the MSXML solution for my driver. For those interested, here's the code I'm using:
这个答案改进了Patrick Cuff的伟大答案,因为它添加了对UTF-的支持8 和 UTF-16 LE 编码(“Unicode”)。(此外,代码已简化)。
示例:
重要提示:
如果您希望能够将所有 Unicode 字符(例如,
€
)表示为文字< /em> 在.vbs
文件中,将其另存为 UTF-16LE(“Unicode”)。如果您的脚本通过
cscript.exe
作为控制台应用程序运行,则并非所有 Unicode 字符都可以正确呈现 - 显示输出(由于字体限制,但您可以复制和粘贴它们),更重要的是,如果您尝试捕获或重定向输出,任何非 ASCII 范围字符控制台 OEM 代码页的一部分实际上丢失(替换为文字?
字符)。This answer improves on Patrick Cuff's great answer in that it adds support for UTF-8 and UTF-16 LE encodings ("Unicode"). (Additionally, the code is streamlined).
Examples:
Important:
If you want to be able to represent all Unicode characters (e.g.,
€
) as literals in your.vbs
file, save it as UTF-16LE ("Unicode").If your script is run as a console application, via
cscript.exe
, not all Unicode characters may render correctly in direct-to-display output (due to font limitations, but you can copy & paste them) and, more importantly, if you try to capture or redirect the output, any non-ASCII-range characters that aren't part of the console's OEM code page are effectively lost (replaced with literal?
characters).无需 ADODB.Stream 和 MSXml2.DOMDocument,即可在纯 vbscript 中对 base64 进行编码。
例如:
如果字符串中存在宽字符(AscW(c) > 255 或 <0),则可以在调用 btoa 之前将其转换为 utf-8。
utf-8转换也可以用纯vb脚本编写。
It's possible to encode base64 in pure vbscript without ADODB.Stream and MSXml2.DOMDocument.
for example:
If there are wide characters (AscW(c) > 255 or < 0) in your string, you can convert it to utf-8 before call btoa.
utf-8 convertion also can be written in pure vbscript.
所以我有一些编码器和解码器的其他完整示例:
编码器:
解码器:
So I have some other full example of encoder and decoder:
Encoder:
Decoder:
这是一个不使用 ADODB 对象的解码示例。
This is a decode example that does not use the ADODB object.
因此,您可以使用此对象来编码或解码 Base64 =
CreateObject("Msxml2.DOMDocument.3.0")
并使用 Array 对其进行编码或解码。
更多信息 VBS_Array
这是我的方式:
So you can use this object to Encode or Decode Base64 =
CreateObject("Msxml2.DOMDocument.3.0")
And use Array to Encode or Decode It.
More info VBS_Array
Here is my way :