正好翻出来一个有意思的点 utf8 bom…- Option Explicit
-
- WScript.Echo base64utf8("5om55aSE55CG5LmL5a62", False) & vbCrLf & _
- base64utf8("批处理之家", True)
-
- '************************************************************************
- '字符串 Base64 编码、解码 (utf-8)
- '************************************************************************
- Function base64utf8(ByVal sText, ByVal bAsEncodeDecode)
- Dim oStream, oXML, oNode
- On Error Resume Next
- Set oStream = CreateObject("ADODB.Stream")
- Set oXML = CreateObject("Msxml2.DOMDocument.3.0")
- Set oNode = oXML.CreateElement("base64")
- oNode.DataType = "bin.base64"
- base64utf8 = ""
- If bAsEncodeDecode Then
- oStream.Type = 2 'adTypeText = 2
- oStream.Charset = "utf-8"
- oStream.Open
- oStream.WriteText sText
- oStream.Position = 0
- oStream.Type = 1 'adTypeBinary = 1
- oStream.Position = 0
- oNode.nodeTypedValue = oStream.Read
- If Err.Number = 0 Then
- base64utf8 = oNode.Text '移除 utf-8 文件的 BMO头 efbbbf。base64_decode('77u/')
- If Left(base64utf8, 4) = "77u/" Then base64utf8 = Mid(base64utf8,5)
- End If
- Else
- oStream.Type = 1 'adTypeBinary = 1
- oStream.Open
- oNode.Text = sText
- oStream.Write oNode.nodeTypedValue
- oStream.Position = 0
- oStream.Type = 2 'adTypeText = 2
- oStream.Charset = "utf-8"
- If Err.Number = 0 Then base64utf8 = oStream.ReadText
- End If
- Set oStream = Nothing
- Set oNode = Nothing
- Set oXML = Nothing
- End Function
复制代码 结果如下:- ---------------------------
- Windows Script Host
- ---------------------------
- 批处理之家
-
- 5om55aSE55CG5LmL5a62
- ---------------------------
- 确定
- ---------------------------
复制代码
|