网上早有人写好了,搜索一下就能得到:- WScript.Echo utf8to16(Base64Decode("5om55aSE55CG5LmL5a62"))
-
- Function utf8to16(str)
- Dim out, i, len1, c, t
- Dim char2, char3
- out = ""
- len1 = Len(str)
- i = 0
- While (i < len1)
- c = Asc(Mid(str, i + 1, 1))
- i = i + 1
- t = c \ 16
- If t >= 0 And t <= 7 Then
- out = out + Mid(str, i, 1)
- ElseIf t = 12 Or t = 13 Then
- char2 = Asc(Mid(str, i + 1, 1))
- i = i + 1
- out = out + Chr(((c And 31) * 64) Or (char2 And 31))
- ElseIf t = 14 Then
- char2 = Asc(Mid(str, i + 1, 1))
- i = i + 1
- char3 = Asc(Mid(str, i + 1, 1))
- i = i + 1
- out = out + ChrW(((c And 15) * 4096) Or ((char2 And 63) * 64) Or ((char3 And 63)))
- End If
- Wend
- utf8to16 = out
- End Function
-
- ' Decodes a base-64 encoded string (BSTR type).
- ' 1999 - 2004 Antonin Foller, http://www.motobit.com
- ' 1.01 - solves problem with Access And 'Compare Database' (InStr)
- Function Base64Decode(ByVal base64String)
- 'rfc1521
- '1999 Antonin Foller, Motobit Software, http://Motobit.cz
- Const Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
- Dim dataLength, sOut, groupBegin
-
- 'remove white spaces, If any
- base64String = Replace(base64String, vbCrLf, "")
- base64String = Replace(base64String, vbTab, "")
- base64String = Replace(base64String, " ", "")
-
- 'The source must consists from groups with Len of 4 chars
- dataLength = Len(base64String)
- If dataLength Mod 4 <> 0 Then
- Err.Raise 1, "Base64Decode", "Bad Base64 string."
- Exit Function
- End If
-
-
- ' Now decode each group:
- For groupBegin = 1 To dataLength Step 4
- Dim numDataBytes, CharCounter, thisChar, thisData, nGroup, pOut
- ' Each data group encodes up To 3 actual bytes.
- numDataBytes = 3
- nGroup = 0
-
- For CharCounter = 0 To 3
- ' Convert each character into 6 bits of data, And add it To
- ' an integer For temporary storage. If a character is a '=', there
- ' is one fewer data byte. (There can only be a maximum of 2 '=' In
- ' the whole string.)
-
- thisChar = Mid(base64String, groupBegin + CharCounter, 1)
-
- If thisChar = "=" Then
- numDataBytes = numDataBytes - 1
- thisData = 0
- Else
- thisData = InStr(1, Base64, thisChar, vbBinaryCompare) - 1
- End If
- If thisData = -1 Then
- Err.Raise 2, "Base64Decode", "Bad character In Base64 string."
- Exit Function
- End If
-
- nGroup = 64 * nGroup + thisData
- Next
-
- 'Hex splits the long To 6 groups with 4 bits
- nGroup = Hex(nGroup)
-
- 'Add leading zeros
- nGroup = String(6 - Len(nGroup), "0") & nGroup
-
- 'Convert the 3 byte hex integer (6 chars) To 3 characters
- pOut = Chr(CByte("&H" & Mid(nGroup, 1, 2))) + _
- Chr(CByte("&H" & Mid(nGroup, 3, 2))) + _
- Chr(CByte("&H" & Mid(nGroup, 5, 2)))
-
- 'add numDataBytes characters To out string
- sOut = sOut & Left(pOut, numDataBytes)
- Next
-
- Base64Decode = sOut
- End Function
复制代码
|