vb6 的 base64转换
时间: 2023-09-12 10:02:39 浏览: 260
VB6.0 RSA/AES/Md5/Base64加密 解密 微信小程序 javascript 通用
VB6 中可以使用 Microsoft XML Core Services (MSXML) 提供的 Base64 编码和解码功能。以下是一个示例代码:
```
Private Function Base64Encode(ByVal sText As String) As String
Dim xmlDoc As MSXML2.DOMDocument60
Dim nodeElem As MSXML2.IXMLDOMElement
Set xmlDoc = New MSXML2.DOMDocument60
Set nodeElem = xmlDoc.createElement("b64")
nodeElem.DataType = "bin.base64"
nodeElem.nodeTypedValue = Stream_StringToBinary(sText)
Base64Encode = nodeElem.Text
Set nodeElem = Nothing
Set xmlDoc = Nothing
End Function
Private Function Base64Decode(ByVal sText As String) As String
Dim xmlDoc As MSXML2.DOMDocument60
Dim nodeElem As MSXML2.IXMLDOMElement
Set xmlDoc = New MSXML2.DOMDocument60
Set nodeElem = xmlDoc.createElement("b64")
nodeElem.DataType = "bin.base64"
nodeElem.Text = sText
Base64Decode = Stream_BinaryToString(nodeElem.nodeTypedValue)
Set nodeElem = Nothing
Set xmlDoc = Nothing
End Function
Private Function Stream_StringToBinary(ByVal sText As String) As Variant
Dim Stream As ADODB.Stream
Set Stream = New ADODB.Stream
Stream.Open
Stream.Type = adTypeText
Stream.Charset = "utf-8"
Stream.WriteText sText
Stream.Position = 0
Stream.Type = adTypeBinary
Stream_StringToBinary = Stream.Read
Stream.Close
Set Stream = Nothing
End Function
Private Function Stream_BinaryToString(ByVal vBinary As Variant) As String
Dim Stream As ADODB.Stream
Set Stream = New ADODB.Stream
Stream.Open
Stream.Type = adTypeBinary
Stream.Write vBinary
Stream.Position = 0
Stream.Type = adTypeText
Stream.Charset = "utf-8"
Stream_BinaryToString = Stream.ReadText
Stream.Close
Set Stream = Nothing
End Function
```
使用示例:
```
Dim sText As String
Dim sBase64 As String
Dim sDecoded As String
sText = "Hello, world!"
sBase64 = Base64Encode(sText)
MsgBox sBase64
sDecoded = Base64Decode(sBase64)
MsgBox sDecoded
```
阅读全文