Created
March 7, 2012 02:36
-
-
Save wangye/1990532 to your computer and use it in GitHub Desktop.
Base64 to Hex or Hex to Base64 encode and decode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' Base64 to Hex or Hex to Base64 encode and decode | |
' Website: http://wangye.org | |
' Base64转16进制 | |
Function B64ToHex(ByVal strContent) | |
Dim i,strReturned, b64pad, _ | |
b64map, chTemp, intLocate, k , slop | |
strReturned = "" : k = 0 | |
b64map="ABCDEFGHIJKLMNOPQRSTUVWXYZ" &_ | |
"abcdefghijklmnopqrstuvwxyz0123456789+/" | |
b64pad="=" | |
For i=0 To Len(strContent)-1 | |
chTemp = Mid(strContent, i+1, 1) | |
If chTemp = b64pad Then Exit For | |
intLocate = InStr(1, b64map, chTemp, 0)-1 | |
If intLocate > -1 Then | |
Select Case K | |
Case 0 | |
strReturned = strReturned &_ | |
Int2Char(Int(intLocate / 4)) | |
slop = intLocate And 3 : k = 1 | |
Case 1 | |
strReturned = strReturned &_ | |
Int2Char( (slop * 4) Or (Int(intLocate / 16)) ) | |
slop = intLocate And &h0f : k = 2 | |
Case 2 | |
strReturned = strReturned &_ | |
Int2Char(slop) & Int2Char(Int(intLocate / 4)) | |
slop = intLocate And 3 : k=3 | |
Case Else | |
strReturned = strReturned &_ | |
Int2Char( (slop * 4) Or (Int(intLocate / 16)) ) &_ | |
Int2Char(intLocate And &h0f) | |
k = 0 | |
End Select | |
End If | |
Next | |
If k=1 Then strReturned = strReturned & Int2Char(slop * 4) | |
B64ToHex = strReturned | |
End Function | |
' 16进制转Base64 | |
Function HexToB64(ByVal strContent) | |
Dim i, c, strReturned, b64map, b64pad, intLen | |
b64map="ABCDEFGHIJKLMNOPQRSTUVWXYZ" &_ | |
"abcdefghijklmnopqrstuvwxyz0123456789+/" | |
b64pad="=" | |
intLen = Len(strContent) | |
For i=0 To intLen-3 Step 3 | |
c=Clng("&h" & Mid(strContent,i+1,3)) | |
strReturned = strReturned &_ | |
Mid(b64map, Int(c / 64 +1), 1) &_ | |
Mid(b64map, (c And 63)+1, 1) | |
Next | |
If i+1=intLen Then | |
c =Clng("&h" & Mid(strContent,i+1,1)) | |
strReturned = strReturned & Mid(b64map, c * 4 + 1, 1) | |
ElseIf i+2=intLen Then | |
c =Clng("&h" & Mid(strContent,i+1,2)) | |
strReturned = strReturned & Mid(b64map, Int(c / 4) + 1, 1) &_ | |
Mid(b64map, (c And 3)*16+1, 1) | |
End If | |
While (Len(strReturned) And 3) >0 | |
strReturned = strReturned & b64pad | |
Wend | |
HexToB64 = strReturned | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment