Base64编码/解码VB6超精简版(适用于中、英文)

上次因为要编写自动登录邮箱的程序,需要Base64编码,但是我看了几种版本的VB下Base64编码的程序,发现要么就是太冗长,要么就是不支持中文,要么根本不能用,于是我想求人不如求己,便仔细研究了一下Base64编码原理,然后编写了这段程序,在电脑上调试通过,可以顺利进行Base64编码/解码,速度也快。现在拿出来分享给大家,希望对大家有所帮助。若有Bug请与我联系:hzh932@gmail.com

程序模块:

Option Explicit
Private Const cstBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
Private arrBase64() As String
'作者:同济黄正 引用请注明出处:http://fine3x.com
'00100001 00100001 00100001             --源码
'00001000 00010010 00000100 00100001    --Base64码

Public Function Base64Encode(strSource As String) As String
On Error Resume Next
'适用于中、英文的Base64编码/解码VB6超精简版 作者:同济黄正 引用请注明出处:http://fine3x.com
If UBound(arrBase64) = -1 Then
    arrBase64 = Split(StrConv(cstBase64, vbUnicode), vbNullChar)
End If
Dim arrB() As Byte, bTmp(2)  As Byte, bT As Byte
Dim I As Long, J As Long
arrB = StrConv(strSource, vbFromUnicode)

J = UBound(arrB)
For I = 0 To J Step 3
    Erase bTmp
    bTmp(0) = arrB(I + 0)
    bTmp(1) = arrB(I + 1)
    bTmp(2) = arrB(I + 2)
   
    bT = (bTmp(0) And 252) / 4
    Base64Encode = Base64Encode & arrBase64(bT)
   
    bT = (bTmp(0) And 3) * 16
    bT = bT + bTmp(1) / 16
    Base64Encode = Base64Encode & arrBase64(bT)
   
    bT = (bTmp(1) And 15) * 4
    bT = bT + bTmp(2) / 64
    If I + 1 <= J Then
        Base64Encode = Base64Encode & arrBase64(bT)
    Else
        Base64Encode = Base64Encode & "="
    End If
   
    bT = bTmp(2) And 63
    If I + 2 <= J Then
        Base64Encode = Base64Encode & arrBase64(bT)
    Else
        Base64Encode = Base64Encode & "="
    End If
Next
End Function

Public Function Base64Decode(strEncoded As String) As String
'适用于中、英文的Base64编码/解码VB6超精简版 作者:同济黄正 引用请注明出处:http://fine3x.com
On Error Resume Next
Dim arrB() As Byte, bTmp(3)  As Byte, bT As Long, bRet() As Byte
Dim I As Long, J As Long
arrB = StrConv(strEncoded, vbFromUnicode)
J = InStr(strEncoded & "=", "=") - 2
ReDim bRet(J - J / 4 - 1)
For I = 0 To J Step 4
    Erase bTmp
    bTmp(0) = (InStr(cstBase64, Chr(arrB(I))) - 1) And 63
    bTmp(1) = (InStr(cstBase64, Chr(arrB(I + 1))) - 1) And 63
    bTmp(2) = (InStr(cstBase64, Chr(arrB(I + 2))) - 1) And 63
    bTmp(3) = (InStr(cstBase64, Chr(arrB(I + 3))) - 1) And 63
   
    bT = bTmp(0) * 2 ^ 18 + bTmp(1) * 2 ^ 12 + bTmp(2) * 2 ^ 6 + bTmp(3)
   
    bRet((I / 4) * 3) = bT / 65536
    bRet((I / 4) * 3 + 1) = (bT And 65280) / 256
    bRet((I / 4) * 3 + 2) = bT And 255
Next
Base64Decode = StrConv(bRet, vbUnicode)
End Function

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
可以使用Pythonbase64库进行图片的编码解码。 以下是一个示例代码,可以将一张图片进行base64编码,并将编码后的结果进行解码并保存为新的图片文件: ``` import base64 # 将图片编码base64格式 with open("image.png", "rb") as image_file: encoded_string = base64.b64encode(image_file.read()).decode('utf-8') # 将base64编码的字符串解码为图片并保存 with open("decoded_image.png", "wb") as output_file: output_file.write(base64.b64decode(encoded_string)) ``` 在上述代码,首先使用`open()`函数打开要编码的图片文件,然后使用`base64.b64encode()`函数将图片内容编码base64格式。编码后的字符串需要使用`decode()`函数转换为普通的字符串,以便后续处理。 接下来,在第二个代码块,我们将编码后的字符串解码为原始的图片内容,并使用`open()`函数将其保存为新的图片文件。在这里,我们使用`wb`模式打开输出文件,以便正确地写入二进制数据。 如果要进行base64解码而不是编码,则可以使用`base64.b64decode()`函数对编码后的字符串进行解码。例如: ``` import base64 # 从 base64 编码的字符串解码出图片内容 with open("base64_encoded_image.txt", "r") as encoded_file: encoded_string = encoded_file.read() decoded_image = base64.b64decode(encoded_string) # 将解码后的图片内容保存为文件 with open("decoded_image.png", "wb") as output_file: output_file.write(decoded_image) ``` 在上述代码,我们首先打开包含base64编码字符串的文件,并使用`read()`函数读取编码后的字符串。然后,我们使用`base64.b64decode()`函数将字符串解码为原始的二进制数据,并将其保存到`decoded_image`变量。最后,我们使用`open()`函数将解码后的二进制数据写入到新的图片文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值