[VBA]如何对二进制字符转化为数值,再将数值转化为二进制字符,重新写到二进制文件
如何对二进制字符转化为数值,再将数值转化为二进制字符,重新写到二进制文件中如何对二进制变量中的二进制字符逐一提取进行转化为数值,然后再将数值转化为二进制字符,重新写到二进制文件中。下面是我测试的过程。<BR>在测试一:读取的二进制变量在degub窗口中显示后,重新输出到另一文件中,输出的文件正确。<BR>在测试二:输出文件已经变化,格式已经变化。<BR>在测试三:可以很清楚看到数值与结果的变化。
我该如何操作才能获得正确的#3文件,请大伙帮帮忙!!!
Const BLOCKSIZE = 4096<BR>Public SFileName As String<BR>Public DFileName As String<BR>Public NewFileName As String
<BR>Private Sub SaveToDF()<BR> '定义数据块数组<BR> Dim byteData() As Byte<BR> '定义数据块个数<BR> Dim NumBlocks As Long<BR> Dim FileLength As Long<BR> '定义剩余字节长度<BR> Dim LeftOver As Long<BR> Dim SourceFile As Long<BR> Dim DiskFile As Long<BR> Dim i As Long<BR> '判断文件是否存在<BR> If Dir(SFileName) <> "" Then<BR> SourceFile = FreeFile<BR> DiskFile = FreeFile<BR> '打开二进制文件<BR> 'Open SFileName For Binary Access Read As SourceFile<BR> 'Open DFileName For Binary Access Write As DiskFile<BR> Open SFileName For Binary Access Read As #1<BR> Open DFileName For Binary Access Write As #2<BR> FileLength = LOF(1)<BR> '判断文件是否空<BR> If FileLength = 0 Then<BR> Close #1<BR> MsgBox SFileName & "文件无内容,请重新指定文件!", vbExclamation, "注意"<BR> Else<BR> '得到数据块的个数<BR> NumBlocks = FileLength \ BLOCKSIZE<BR> '得到剩余字节数<BR> LeftOver = FileLength Mod BLOCKSIZE<BR> 'Fld.Value = Null<BR> <BR> Debug.Print "FileLength=" & FileLength, "NumBlocks=" & NumBlocks, "LeftOver =" & LeftOver<BR> ReDim byteData(BLOCKSIZE)<BR> For i = 1 To NumBlocks '测试一<BR> Get #1, , byteData()<BR> 'Debug.Print byteData()<BR> Put #2, , byteData()<BR> Call NewFile(byteData(), 4096)<BR> DoEvents<BR> Next i<BR> '将剩余数据写入FLD<BR> ReDim byteData(LeftOver)<BR> Get #1, , byteData()<BR> 'Debug.Print byteData()<BR> Put #2, , byteData()<BR> Call NewFile(byteData(), LeftOver)<BR> 'Fld.AppendChunk byteData()<BR> Close #1<BR> Close #2<BR> End If<BR> Else<BR> MsgBox "文件不存在,请重新指定文件!", vbExclamation, "注意"<BR> End If<BR>End Sub<BR>Private Function GetFileName() As String<BR> <BR> CommonDialog1.CancelError = True<BR>On Error GoTo CancelErr<BR> CommonDialog1.Filter = "所有文件(*.*)|*.*"<BR> CommonDialog1.ShowOpen<BR> GetFileName = CommonDialog1.FileName<BR> Exit Function<BR>CancelErr:<BR> GetFileName = ""<BR> <BR>End Function
<BR>Private Sub Command1_Click()<BR> SFileName = GetFileName<BR> Label1.Caption = SFileName<BR> Command2.Enabled = True<BR> Command1.Enabled = False<BR> <BR>End Sub
Private Sub Command2_Click()<BR>'我想在这里通过加密手段处理一下数据,必须把二进制字符变为相应的数值,然后加密,解密,输出到新的文件中。请各位指点!!!<BR>DFileName = GetFileName<BR>Label2.Caption = DFileName<BR>NewFileName = App.Path & "/123." & Right(DFileName, 3)<BR>Open NewFileName For Binary Access Write As #3 '测试二<BR>Call SaveToDF<BR>Close #3<BR>Command1.Enabled = True<BR>Command2.Enabled = False<BR>End Sub
Private Sub Form_Load()<BR>Command1.Enabled = True<BR>Command2.Enabled = False<BR>Close All<BR>End Sub
Public Sub NewFile(Data() As Byte, FLenght As Long)<BR>'求救!!!<BR>Dim StrNum As Long<BR>Dim NewStr As String <BR>Dim ii As Long<BR>For ii = 1 To FLenght<BR>StrNum = AscW(Data(ii))<BR>NewStr = Str(StrNum)<BR>Put #3, , NewStr<BR>Debug.Print Data(ii) & "-" & StrNum & "-" & NewStr '测试三<BR>Next ii<BR>End Sub<BR>
页:
[1]