Base64在VBScript中编码字符串 [英] Base64 Encode String in VBScript

查看:247
本文介绍了Base64在VBScript中编码字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web服务加载驱动程序是Windows脚本文件(WSF),包括一些VBScript和JavaScript文件。我的Web服务要求传入的消息是base64编码的。我目前有一个VBScript函数,但这是非常低效的(内存密集型,主要是由于VBScripts可怕的字符串连接)



[Aside;是的,我看到杰夫的最新博文。连接发生在一个循环中,大小为1,000到10,000的字节的消息。]



我尝试使用一些自定义字符串连接例程一个使用数组,一个使用ADODB.Stream。这些帮助,有一点,但是我认为如果我有其他方式对邮件进行编码,而不是通过我自己的VBS函数,这将有助于更多。



还有其他一些我的消息编码方式,优先使用本机Windows方法?

解决方案

我最初是使用Antonin Foller的一些VBScript代码:
Base64编码VBS功能 Base64解码VBS功能



搜索Antonin的网站,我看到他有一些代码引用的可打印编码,使用CDO.Message对象,所以我尝试了。



最后,我移植了马克对VBScript的回答(也使用了这个 SO问题),并使用 Stream___StringToBinary Stream_BinaryToString 来自Antonin网站的功能,以获取使用MSXML编码的功能。



我运行了一个快速测试,以测量1500字符消息的编码时间(我需要发送到我的Web服务的平均消息大小)跨所有四种方法:




  • ive VBScript(VBScript)

  • 引用可打印,使用CDO.Message(QP)

  • 引用的可打印二进制,使用CDO.Message(QP Binary) / li>
  • MSXML / ADODB.Stream(MSXML)



以下是结果:

 
迭代:10,000
消息大小:1,500

+ ------------ - + ----------- +
+方法|时间(ms)+
+ ------------- + ----------- +
| VBScript | 301,391 |
+ ------------- + ----------- +
| QP | 12,922 |
+ ------------- + ----------- +
| QP(二进制)| 13,953 |
+ ------------- + ----------- +
| MSXML | 3,312 |
+ ------------- + ----------- +

我也在测试运行时监视内存利用率(Windows任务管理器中的cscript.exe进程的Mem Usage)。我没有任何原始数字,但引用的可打印和MSXML解决方案的内存利用率低于VBScript解决方案(前者为7000,000,VBScript约为16,000K)。



我决定用我的驱动程序的MSXML解决方案。对于那些感兴趣的人,这里是我使用的代码:

  base64.vbs 
函数Base64Encode(sText)
Dim oXML,oNode

设置oXML = CreateObject(Msxml2.DOMDocument.3.0)
设置oNode = oXML.CreateElement(base64)
oNode.dataType = bin.base64
oNode.nodeTypedValue = Stream_StringToBinary(sText)
Base64Encode = oNode.text
设置oNode = Nothing
设置oXML = Nothing
结束函数

功能Base64Decode(ByVal vCode)
Dim oXML,oNode

设置oXML = CreateObject(Msxml2.DOMDocument.3.0)
设置oNode = oXML。 CreateElement(base64)
oNode.dataType =bin.base64
oNode.text = vCode
Base64Decode = Stream_BinaryToString(oNode.nodeTypedValue)
设置oNode = Nothing
设置oXML =没有
结束函数

'Stream_StringToBinary函数
'2003 Antonin Foller,http://www.motobit.com
'文本 - 字符串参数Ť o转换为二进制数据
函数Stream_StringToBinary(Text)
Const adTypeText = 2
Const adTypeBinary = 1

'创建流对象
Dim BinaryStream'As New Stream
设置BinaryStream = CreateObject(ADODB.Stream)

'指定流类型 - 我们要保存文本/字符串数据。
BinaryStream.Type = adTypeText

'指定字符集对于源文本(unicode)数据。
BinaryStream.CharSet =us-ascii

'打开流并写入文本/字符串数据对象
BinaryStream.Open
BinaryStream.WriteText Text

'更改流类型到二进制
BinaryStream.Position = 0
BinaryStream.Type = adTypeBinary

'忽略前两个字节 - $ b $的符号b BinaryStream.Position = 0

'打开流并从对象中获取二进制数据
Stream_StringToBinary = BinaryStream.Read

设置BinaryStream = Nothing
结束函数

'Stream_BinaryToString函数
'2003 Antonin Foller,http://www.motobit.com
'二进制 - VT_UI1 | VT_ARRAY数据转换为字符串
函数Stream_BinaryToString(二进制)
Const adTypeText = 2
Const adTypeBinary = 1

'创建流对象
Dim BinaryStream '作为新流
设置BinaryStream = CreateObject(ADODB.Stream)

'指定流类型 - 我们要保存二进制数据。
BinaryStream.Type = adTypeBinary

'打开流并写入二进制数据对象
BinaryStream.Open
BinaryStream.Write二进制

'更改流类型到文本/字符串
BinaryStream.Position = 0
BinaryStream.Type = adTypeText

'指定字符集对于输出文本(unicode)数据。
BinaryStream.CharSet =us-ascii

'打开流并从对象获取文本/字符串数据
Stream_BinaryToString = BinaryStream.ReadText
设置BinaryStream =没有
结束功能


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?

解决方案

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:

  • Native VBScript (VBScript)
  • Quoted Printable, using CDO.Message (QP)
  • Quoted Printable Binary, using CDO.Message (QP Binary)
  • MSXML/ADODB.Stream (MSXML)

Here are the results:

Iterations   : 10,000
Message Size :  1,500

+-------------+-----------+
+ Method      | Time (ms) + 
+-------------+-----------+
| VBScript    |   301,391 |
+-------------+-----------+
| QP          |    12,922 |
+-------------+-----------+
| QP (Binary) |    13,953 |
+-------------+-----------+
| MSXML       |     3,312 |
+-------------+-----------+

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:

base64.vbs
Function Base64Encode(sText)
    Dim oXML, oNode

    Set oXML = CreateObject("Msxml2.DOMDocument.3.0")
    Set oNode = oXML.CreateElement("base64")
    oNode.dataType = "bin.base64"
    oNode.nodeTypedValue =Stream_StringToBinary(sText)
    Base64Encode = oNode.text
    Set oNode = Nothing
    Set oXML = Nothing
End Function

Function Base64Decode(ByVal vCode)
    Dim oXML, oNode

    Set oXML = CreateObject("Msxml2.DOMDocument.3.0")
    Set oNode = oXML.CreateElement("base64")
    oNode.dataType = "bin.base64"
    oNode.text = vCode
    Base64Decode = Stream_BinaryToString(oNode.nodeTypedValue)
    Set oNode = Nothing
    Set oXML = Nothing
End Function

'Stream_StringToBinary Function
'2003 Antonin Foller, http://www.motobit.com
'Text - string parameter To convert To binary data
Function Stream_StringToBinary(Text)
  Const adTypeText = 2
  Const adTypeBinary = 1

  'Create Stream object
  Dim BinaryStream 'As New Stream
  Set BinaryStream = CreateObject("ADODB.Stream")

  'Specify stream type - we want To save text/string data.
  BinaryStream.Type = adTypeText

  'Specify charset For the source text (unicode) data.
  BinaryStream.CharSet = "us-ascii"

  'Open the stream And write text/string data To the object
  BinaryStream.Open
  BinaryStream.WriteText Text

  'Change stream type To binary
  BinaryStream.Position = 0
  BinaryStream.Type = adTypeBinary

  'Ignore first two bytes - sign of
  BinaryStream.Position = 0

  'Open the stream And get binary data from the object
  Stream_StringToBinary = BinaryStream.Read

  Set BinaryStream = Nothing
End Function

'Stream_BinaryToString Function
'2003 Antonin Foller, http://www.motobit.com
'Binary - VT_UI1 | VT_ARRAY data To convert To a string 
Function Stream_BinaryToString(Binary)
  Const adTypeText = 2
  Const adTypeBinary = 1

  'Create Stream object
  Dim BinaryStream 'As New Stream
  Set BinaryStream = CreateObject("ADODB.Stream")

  'Specify stream type - we want To save binary data.
  BinaryStream.Type = adTypeBinary

  'Open the stream And write binary data To the object
  BinaryStream.Open
  BinaryStream.Write Binary

  'Change stream type To text/string
  BinaryStream.Position = 0
  BinaryStream.Type = adTypeText

  'Specify charset For the output text (unicode) data.
  BinaryStream.CharSet = "us-ascii"

  'Open the stream And get text/string data from the object
  Stream_BinaryToString = BinaryStream.ReadText
  Set BinaryStream = Nothing
End Function

这篇关于Base64在VBScript中编码字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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