您好!VB6.0与Base64编码函数对应的解码函数有吗?与Function Base64Encode对应的

作者&投稿:初明 (若有异议请与网页底部的电邮联系)
VB6.0 Base64编码问题~

dim encode as string
以UTF-8编码形式创建“中国”文本 然后再调用ToBase64String去转换

'Base64编码函数,参数sFileName:与编码文件路径,sCodeName:编码文件存储路径
Function Base64Encode(ByVal sFileName As String, ByVal sCodeName As String) As String
Dim sCodeTable(0 To 63) As Byte '编码表
Dim tmpBytes3(1 To 3) As Byte '临时存储原字节码
Dim tmpBytes4(1 To 4) As Byte '临时存储字节分解吗

Dim i As Long, k As Long
Dim sFileLen As Long '文件长度
Dim n As Long
Dim m As Long

'初始化编码表
For i = 0 To 25
sCodeTable(i) = i + 65 '大写字母
sCodeTable(i + 26) = i + 97 '小写字母
Next
For i = 52 To 61
sCodeTable(i) = i - 4
Next
sCodeTable(62) = Asc("+")
sCodeTable(63) = Asc("/")

'文件长度
sFileLen = FileLen(sFileName)
n = sFileLen \ 3 '整除3
m = sFileLen Mod 3 '除以3的余数

'打开文件
Open sFileName For Binary As #1 Len = 32760
Open sCodeName For Binary As #2 Len = 32760

For i = 1 To n
Get #1, , tmpBytes3 '读取3个字节到tmpBytes
'八3个字节分解为4个字节
tmpBytes4(1) = (tmpBytes3(1) And 252) / 4 '截取前6bit
tmpBytes4(2) = (tmpBytes3(1) And 3) * 16 + (tmpBytes3(2) And 240) / 16 '截取第二个6bit
tmpBytes4(3) = (tmpBytes3(2) And 15) * 4 + (tmpBytes3(3) And 192) / 64 '截取第三个6bit
tmpBytes4(4) = tmpBytes3(3) And 63 '截取第三个6bit
For k = 1 To 4
tmpBytes4(k) = sCodeTable(tmpBytes4(k))
Next
Put #2, , tmpBytes4 '将编码写入编码文件

DoEvents '测试时使用,编译时可以注释掉===========================================
Next

'如果文件大小不是3的整数倍
If m = 1 Then
Get #1, , tmpBytes3(1)
tmpBytes3(2) = 0
tmpBytes4(1) = (tmpBytes3(1) And 252) / 4 '截取前6bit
tmpBytes4(2) = (tmpBytes3(1) And 3) * 16 + (tmpBytes3(2) And 240) / 16 '截取第二个6bit
Put #2, , sCodeTable(tmpBytes4(1))
Put #2, , sCodeTable(tmpBytes4(2))
Put #2, , CByte(Asc("="))
Put #2, , CByte(Asc("="))
ElseIf m = 2 Then
Get #1, , tmpBytes3(1)
Get #1, , tmpBytes3(2)
tmpBytes3(3) = 0
tmpBytes4(1) = (tmpBytes3(1) And 252) / 4 '截取前6bit
tmpBytes4(2) = (tmpBytes3(1) And 3) * 16 + (tmpBytes3(2) And 240) / 16 '截取第二个6bit
tmpBytes4(3) = (tmpBytes3(2) And 15) * 4 + (tmpBytes3(3) And 192) / 64 '截取第三个6bit
Put #2, , sCodeTable(tmpBytes4(1))
Put #2, , sCodeTable(tmpBytes4(2))
Put #2, , sCodeTable(tmpBytes4(3))
Put #2, , CByte(Asc("="))
End If
'关闭文件
Close #2
Close #1

End Function

Base64解码函数(对Base64编码文件进行解码)

'==============================================================================================
'函数名称:Base64Decode
'函数功能:Base64解码函数
'入口参数:sCodeName,欲解码文件路径;sFileName,解码后文件存储路径
'**返回值:解码成功则返回True,解码失败则返回False(例如sFileName已存在或者sCodeName不存在等等)
'
'特别注意:解码后的文件名由用户自己定义,但扩展名一定要符合,不然的的话,解码之后可能不能识别
'
'==============================================================================================
Function
Base64Decode(ByVal sCodeName As String, ByVal sFileName As String) As
Boolean
Dim tmpBytes3(1 To 3) As Byte
Dim tmpBytes4(1 To 4) As
Byte

Dim BufferBytes3(1 To 3072) As Byte '建立缓冲字节3KB
Dim
BufferBytes4(1 To 4096) As Byte '建立缓冲字节4KB

Dim i As Long, j As
Long, k As Long, p As Long
Dim sFileLen As Long
'文件长度
Dim n As Long
Dim m As Long
Base64Decode =
False '设置函数为False

'如果待解码文件不存在
If
Dir(sCodeName) = "" Then
MsgBox "文件" & """" & sCodeName &
"""" & "不存在", vbInformation, "错误"
Exit Function
End
If
'如果目标文件已存在
If Dir(sFileName) <> "" Then
If
MsgBox("文件" & """" & sFileName & """" & "已经存在,是否替换", vbQuestion
+ vbYesNo + vbDefaultButton2, "文件已存在") = vbYes Then

'清空文件
Open sFileName For Output As #1
Close
#1
Else
Exit Function
End If
End
If

'文件长度
sFileLen = FileLen(sCodeName)
n = sFileLen
\ 4 '整除4
m = sFileLen Mod 4 '除以4的余数
If m <> 0 Or
sFileLen = 0 Then
MsgBox "文件" & """" & sCodeName & """"
& "的字节数不是4的倍数,无法进行Base64解码", vbInformation, "错误"
Exit
Function
End If
p = n \ 1024 '使用缓冲次数
n = n Mod
1024 '不适用缓冲的次数
If n = 0 Then
p = p - 1
n =
1024
End If

'打开文件
Open sCodeName For Binary As
#1
Open sFileName For Binary As #2
On Error GoTo ErrTo:

'使用缓冲的部分
For i = 1 To p
Get #1, ,
BufferBytes4
'还原为4字节码
For j = 1 To 4096
If
BufferBytes4(j) >= 97 And BufferBytes4(j) <= 122 Then

BufferBytes4(j) = BufferBytes4(j) - 71
ElseIf BufferBytes4(j)
>= 65 And BufferBytes4(j) <= 90 Then
BufferBytes4(j) =
BufferBytes4(j) - 65
ElseIf BufferBytes4(j) >= 48 And
BufferBytes4(j) <= 57 Then
BufferBytes4(j) =
BufferBytes4(j) + 4
ElseIf BufferBytes4(j) = 47
Then
BufferBytes4(j) = 63
ElseIf
BufferBytes4(j) = 43 Then
BufferBytes4(j) = 62

Else
MsgBox "出现非Base64编码字符,无法继续解码", vbInformation,
"错误"
GoTo ErrTo:
End If

Next
'还原为3字节码
For j = 1 To 1024

BufferBytes3(j * 3 - 2) = BufferBytes4(j * 4 - 3) * 4 + (BufferBytes4(j * 4 - 2)
And 48) / 16
BufferBytes3(j * 3 - 1) = (BufferBytes4(j * 4 - 2)
And 15) * 16 + (BufferBytes4(j * 4 - 1) And 60) / 4

BufferBytes3(j * 3) = (BufferBytes4(j * 4 - 1) And 3) * 64 + BufferBytes4(j *
4)
Next
'将缓冲写到文件
Put #2, , BufferBytes3

Next

'不使用缓冲的部分
For i = 1 To n - 1
Get #1, ,
tmpBytes4
'还原为4字节码
For j = 1 To 4
If
tmpBytes4(j) >= 97 And tmpBytes4(j) <= 122 Then

tmpBytes4(j) = tmpBytes4(j) - 71
ElseIf tmpBytes4(j) >= 65 And
tmpBytes4(j) <= 90 Then
tmpBytes4(j) = tmpBytes4(j) -
65
ElseIf tmpBytes4(j) >= 48 And tmpBytes4(j) <= 57
Then
tmpBytes4(j) = tmpBytes4(j) + 4
ElseIf
tmpBytes4(j) = 47 Then
tmpBytes4(j) = 63

ElseIf tmpBytes4(j) = 43 Then
tmpBytes4(j) =
62
Else
MsgBox "出现非Base64编码字符,无法继续解码",
vbInformation, "错误"
GoTo ErrTo:
End
If
Next
'还原为3字节码
tmpBytes3(1) = tmpBytes4(1) *
4 + (tmpBytes4(2) And 48) / 16
tmpBytes3(2) = (tmpBytes4(2) And 15) *
16 + (tmpBytes4(3) And 60) / 4
tmpBytes3(3) = (tmpBytes4(3) And 3) *
64 + tmpBytes4(4)
Put #2, , tmpBytes3
Next

'最后4个字节
Dim tNum As Long
tNum = 0 '记录"="的个数
Get #1, ,
tmpBytes4
If tmpBytes4(4) = 61 Then
tNum = tNum + 1

If tmpBytes4(3) = 61 Then tNum = tNum + 1
End If
For j = 1 To 4 -
tNum
If tmpBytes4(j) >= 97 And tmpBytes4(j) <= 122
Then
tmpBytes4(j) = tmpBytes4(j) - 71
ElseIf
tmpBytes4(j) >= 65 And tmpBytes4(j) <= 90 Then
tmpBytes4(j)
= tmpBytes4(j) - 65
ElseIf tmpBytes4(j) >= 48 And tmpBytes4(j)
<= 57 Then
tmpBytes4(j) = tmpBytes4(j) + 4
ElseIf
tmpBytes4(j) = 47 Then
tmpBytes4(j) = 63
ElseIf
tmpBytes4(j) = 43 Then
tmpBytes4(j) = 62

Else
MsgBox "出现非Base64编码字符,无法继续解码", vbInformation,
"错误"
GoTo ErrTo:
End If
Next

tmpBytes3(1) = tmpBytes4(1) * 4 + (tmpBytes4(2) And 48) / 16
Put #2, ,
tmpBytes3(1)
If tNum < 2 Then
tmpBytes3(2) = (tmpBytes4(2)
And 15) * 16 + (tmpBytes4(3) And 60) / 4
Put #2, ,
tmpBytes3(2)
End If
If tNum < 1 Then
tmpBytes3(3) =
(tmpBytes4(3) And 3) * 64 + tmpBytes4(4)
Put #2, ,
tmpBytes3(3)
End If
Base64Decode = True
ErrTo:
Close
#1
Close #2
End Function


请教VB6.0下标越界问题
这个是一个数组的问题。我们先看第一个下标越界。第一个下标越界是发生在重新定义A、B两个数组,使其长度为N-1的时候。那么我们来看此时的N来源。Dim N As Integer ,此时N为0,经过清点文本内行首字符为“<>”后,N被重新赋值,此时的N是一个计数器。如果,文本内所有行首均无“<>”,那么Re...

VB6.0三个数排序完全代码
Private Sub Command1_Click()a = Val(InputBox("请输入第一个数:"))b = Val(InputBox("请输入第二个数:"))c = Val(InputBox("请输入第三个数:"))s = "输入的三个数为:" & a & ", " & b & ", " & c & vbCrLf If a > b Then t = a: a = b: b = t If a ...

VB6.0引用内存错误
就会出现“0x004581f1指令引用的0x00000000内存,该内存不能为read”,刚开始我还重装了VB6.0,结果还....例六:一个朋友发信息过来,我的电脑便出现了错误信息:“0*772b548f”指令引用的“0*00303033”内存...内存和主板没插好或和其它硬件不兼容等--重插内存或换个插糟 硬盘有问题--更换硬盘 驱动问题--...

vb6.0安装向导在初始化时遇到了致命错误。
2.然后会弹出vb的安装界面,这里不需要直接点击下一步。3.选择接受协议,然后下一步,如下图所示。4.然后在对话框的ID栏中填写123-1111111。其余选项不需要填写。5.然后进入新页面,点击下一步开始安装vb6.0中文企业版 6.然后会来到安装位置的选择界面,根据自己的需要进行设置就行了,设置好的...

VB6.0 怎么连接access已经建立好的表格?请在每条语句后面加上注解,小弟...
Fields(i).Name & ":" & Recordset.Fields(i).Value & Space(25 - Len(Recordset.Fields(i).Value))End If '输出jsb表中各字段 Next i Recordset.MoveNext Loop Text7.Text = tex End Sub 这是一个留言板的增加和显示的例子,表名为"liuyan",有姓名,性别等字段 仅供参考 ...

VB6.0-文件操作
再读取字符。因而在记录长度变动较大的情况,用二进制存取方式可节约存储空间。但另一方面,因为它没有固定长度的记录,不能向随机文件那样任意取出第几条记录,必须建立一个索引表来指示每个记录的起始地址,这就给编程带来了困难。http:\/\/hi.baidu.com\/gagaga0\/item\/16543a37368bdbf62784f4b4 ...

怎么用VB6.0编写病毒(要摸版)
介绍几个用vb编写的病毒以下两个怎么都出现错误呀... 介绍几个用vb 编写的病毒以下两个怎么都出现错误呀 展开  我来答 2个回答 #热议# 职场上受委屈要不要为自己解释?s_b_w 2007-09-01 · TA获得超过365个赞 知道小有建树答主 回答量:232 采纳率:0% 帮助的人:80.7万 我也去答题访问个人...

主机用vb6.0的WINSOCK控件与另一台PC机以太网连接?
可以,1. 简单:两个winsock,用不同的端口号, 比如 端口5000 和触摸屏机联, 端口5010和另一台PC机连接,2. 麻烦点,一个winsock,起始index为0, 相同端口, 程序略麻烦点, 同一个接听端口听到一个连接请求,处理一个,再来一个,再处理一个,端口同但连接的index不同, 是不同的连接。

vb6.0中用 a=ab:b=a\\b:a=a\\b (a,b为整形变量且不为零) 的方法交换变量...
a=a*b :b=a\\b:a=a\\b 记得采纳啊

vb6.0 排列组合
1、排列组合(abc acb bac)2、逆序输出(bca cab cba)细心的你一定发现了,后三组是前三组的逆序输出,因此只排列前一半即可。显然,用循环语句来实现这个功能,具体语言格式我也想不起来了,就说说思路吧。先定义三个数1,2,3。第一轮循环,令a=1,b=2,c=3,然后按从小到大输出;第二轮...

定日县17366725306: VB6.0 Base64编码问题 -
魏蚂宁神: dim encode as string 以UTF-8编码形式创建“中国”文本 然后再调用ToBase64String去转换

定日县17366725306: VB6.0字符是采用什么编码方式来表达和存储的 -
魏蚂宁神: VB6.0中采用Unicode编码来存储和操作字符串,但是在系统中却采用DBCS编码方式,因此当两种编码方式同时出现时,便会出现错误,可以在手机知网里面看一下的,亲,我的回答你是否满意呢

定日县17366725306: vb 6.0 中字符采用什么编码方式.谢谢!
魏蚂宁神: VB6.0中采用Unicode编码来存储和操作字符串

定日县17366725306: 求教VB6.0关于BASE64加密算法错误问题 -
魏蚂宁神: Option Explicit Private Const BASE64CHR As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" Private psBase64Chr(0 To 63) As String'从一个经过Base64的字符串中解码到源字符串 Public ...

定日县17366725306: VB6.0编程代码 -
魏蚂宁神: im a(100) 'a()数据'此处来赋值 Dim b(5) For i = 0 To 99 If a(i) < 6 Then b(0) = b(0) + 1 Else b(Int(a(i) - 5)) = b(Int(a(i) - 5)) End If Next i Print "<6.00", "6~6.99", "7~7.99", "8~8.99", "9~9.99" For i = 0 To 4 Print b(i),Next i

定日县17366725306: VB6.0 帮我编个代码! -
魏蚂宁神: 2. a=cint(rnd*10) b=cint(rnd*10) c=val(a)+val(b) me.print print a print b...

定日县17366725306: VB6.0怎么输入编码 -
魏蚂宁神: B是靠事件驱动的,比如,双击这个按钮,就可以进行这个按钮单击事件编码的输入,你在窗体中加入一个按钮.不同的事件有不同的代码

定日县17366725306: vb6.0是什么? -
魏蚂宁神: VB6.0是微软公司的一个软件开发平台,主要就是用来编程的,适合初学者学习使用,现在外面程序语言很多,各有千秋,学习什么,就看你的爱好了

定日县17366725306: vb6.0中,如何读取sqlserver数据库中image类型的数据,要将数据转换成base64格式的字符串? -
魏蚂宁神: 采用头文件的目的主要是为了使某些定义可以供多个不同 的C源程序使用.因为在需要用到这些定义的C源程序中,只需加上一条#include语句即可,而不必再在此文件中将这些定义重复一遍.预编译程序将把头文件中的定义统统都加入到它 所产生的输出文件中,以供编译程序对之进行处理.

定日县17366725306: 求VB6.0打印及打印设置代码 -
魏蚂宁神: Option ExplicitConst N = 1000 '1000个数Private Sub Form_Activate() Dim i As Long, sum As Long For i = 1 To N If Fc(i) Then Print i sum = sum + i End If Next Print "和为:" & sumEnd SubFunction Fc(m) As Boolean Dim sum As Long, i As ...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网