明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2824|回复: 0

[VBA]如何对二进制字符转化为数值,再将数值转化为二进制字符,重新写到二进制文件

[复制链接]
发表于 2004-8-2 19:25:00 | 显示全部楼层 |阅读模式
[VBA]如何对二进制字符转化为数值,再将数值转化为二进制字符,重新写到二进制文件中[br]如何对二进制变量中的二进制字符逐一提取进行转化为数值,然后再将数值转化为二进制字符,重新写到二进制文件中。 下面是我测试的过程。
在测试一:读取的二进制变量在degub窗口中显示后,重新输出到另一文件中,输出的文件正确。
在测试二:输出文件已经变化,格式已经变化。
在测试三:可以很清楚看到数值与结果的变化。 我该如何操作才能获得正确的#3文件,请大伙帮帮忙!!! Const BLOCKSIZE = 4096
Public SFileName As String
Public DFileName As String
Public NewFileName As String
Private Sub SaveToDF()
'定义数据块数组
Dim byteData() As Byte
'定义数据块个数
Dim NumBlocks As Long
Dim FileLength As Long
'定义剩余字节长度
Dim LeftOver As Long
Dim SourceFile As Long
Dim DiskFile As Long
Dim i As Long
'判断文件是否存在
If Dir(SFileName) <> "" Then
SourceFile = FreeFile
DiskFile = FreeFile
'打开二进制文件
'Open SFileName For Binary Access Read As SourceFile
'Open DFileName For Binary Access Write As DiskFile
Open SFileName For Binary Access Read As #1
Open DFileName For Binary Access Write As #2
FileLength = LOF(1)
'判断文件是否空
If FileLength = 0 Then
Close #1
MsgBox SFileName & "文件无内容,请重新指定文件!", vbExclamation, "注意"
Else
'得到数据块的个数
NumBlocks = FileLength \ BLOCKSIZE
'得到剩余字节数
LeftOver = FileLength Mod BLOCKSIZE
'Fld.Value = Null

Debug.Print "FileLength=" & FileLength, "NumBlocks=" & NumBlocks, "LeftOver =" & LeftOver
ReDim byteData(BLOCKSIZE)
For i = 1 To NumBlocks '测试一
Get #1, , byteData()
'Debug.Print byteData()
Put #2, , byteData()
Call NewFile(byteData(), 4096)
DoEvents
Next i
'将剩余数据写入FLD
ReDim byteData(LeftOver)
Get #1, , byteData()
'Debug.Print byteData()
Put #2, , byteData()
Call NewFile(byteData(), LeftOver)
'Fld.AppendChunk byteData()
Close #1
Close #2
End If
Else
MsgBox "文件不存在,请重新指定文件!", vbExclamation, "注意"
End If
End Sub
Private Function GetFileName() As String

CommonDialog1.CancelError = True
On Error GoTo CancelErr
CommonDialog1.Filter = "所有文件(*.*)|*.*"
CommonDialog1.ShowOpen
GetFileName = CommonDialog1.FileName
Exit Function
CancelErr:
GetFileName = ""

End Function
Private Sub Command1_Click()
SFileName = GetFileName
Label1.Caption = SFileName
Command2.Enabled = True
Command1.Enabled = False

End Sub Private Sub Command2_Click()
'我想在这里通过加密手段处理一下数据,必须把二进制字符变为相应的数值,然后加密,解密,输出到新的文件中。请各位指点!!!
DFileName = GetFileName
Label2.Caption = DFileName
NewFileName = App.Path & "/123." & Right(DFileName, 3)
Open NewFileName For Binary Access Write As #3 '测试二
Call SaveToDF
Close #3
Command1.Enabled = True
Command2.Enabled = False
End Sub Private Sub Form_Load()
Command1.Enabled = True
Command2.Enabled = False
Close All
End Sub Public Sub NewFile(Data() As Byte, FLenght As Long)
'求救!!!
Dim StrNum As Long
Dim NewStr As String
Dim ii As Long
For ii = 1 To FLenght
StrNum = AscW(Data(ii))
NewStr = Str(StrNum)
Put #3, , NewStr
Debug.Print Data(ii) & "-" & StrNum & "-" & NewStr '测试三
Next ii
End Sub
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-26 11:26 , Processed in 0.161697 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表