- 积分
- 1173
- 明经币
- 个
- 注册时间
- 2002-5-24
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
[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
|
|