同志们,我发表点意见,曾经写过一个SAMSUNG DIY VB的软件,那里面就使用了类似文件操作,这里先更正一个问题! 楼主所看到的,是一个二进制文件,只是编辑器以16进制表示了而已。 我翻看了以下以前的代码,给楼主贴一点 自己修改一下以下代码: '文件长度 Private myFileLen As Long '打开的文件 Private FilePathName As String '文件存储数组 Private BuffArray() As Byte '将文件读入一个二进制数组 Private sub FileOpen_2 () Open FilePathName For Binary As #1 myFileLen = LOF(1) ReDim BuffArray(myFileLen - 1) Get #1, , BuffArray Close #1 end sub'把数组写回去的代码 Open FilePathName For Binary Access Write As #1 Put #1, , BuffArray Close #1'你可以用下面的代码把你获得的16进制转换为2进制,然后写回去看看,是不是和原来一样! Function Hex2Bin(InputData As String) As String On Error Resume Next Dim I As Integer Dim BinOut As String Dim Lenhex As IntegerInputData = UCase(InputData) Lenhex = Len(InputData)BinOut = ""For I = 1 To LenhexIf Mid(InputData, I, 1) = "0" Then BinOut = BinOut + "0000" ElseIf Mid(InputData, I, 1) = "1" Then BinOut = BinOut + "0001" ElseIf Mid(InputData, I, 1) = "2" Then BinOut = BinOut + "0010" ElseIf Mid(InputData, I, 1) = "3" Then BinOut = BinOut + "0011" ElseIf Mid(InputData, I, 1) = "4" Then BinOut = BinOut + "0100" ElseIf Mid(InputData, I, 1) = "5" Then BinOut = BinOut + "0101" ElseIf Mid(InputData, I, 1) = "6" Then BinOut = BinOut + "0110" ElseIf Mid(InputData, I, 1) = "7" Then BinOut = BinOut + "0111" ElseIf Mid(InputData, I, 1) = "8" Then BinOut = BinOut + "1000" ElseIf Mid(InputData, I, 1) = "9" Then BinOut = BinOut + "1001" ElseIf Mid(InputData, I, 1) = "A" Then BinOut = BinOut + "1010" ElseIf Mid(InputData, I, 1) = "B" Then BinOut = BinOut + "1011" ElseIf Mid(InputData, I, 1) = "C" Then BinOut = BinOut + "1100" ElseIf Mid(InputData, I, 1) = "D" Then BinOut = BinOut + "1101" ElseIf Mid(InputData, I, 1) = "E" Then BinOut = BinOut + "1110" ElseIf Mid(InputData, I, 1) = "F" Then BinOut = BinOut + "1111" ElseEnd IfNext IHex2Bin = BinOut eds: End Function哎呀,丢人了啊,呵呵,,,,,,,,以前的代码还真有初学者的风范...散了, 楼主问题的解决办法,就是把每一个字符转换为二进制,写入一个数组,然后把按上面方法写回
关键是数字到十六进制字符串的转换
可以用Hex函数
如果数据量多,要求效率,可以用查表法
设置字符串数组,索引是数字,里面的值是对应的十六进制字符串
If InStr(stropenpath, ".dat") <> 0 Then
fso.DeleteFile (stropenpath)
fso.CreateTextFile stropenpath
Set fil = fso.GetFile(stropenpath)
Set ts = fil.OpenAsTextStream(ForWriting)
ts.Write (sdata)
ts.Close
我是这样写的,好像函数不对,写进去的是ascii值,如果是16进制数用VC可以查看出16进制的格试的
000000 d0 cf b1 e0 c1 b1 1a d1 00 20 00 05 00 00 00 00 ...............
000010 d0 cf a1 e0 c1 b1 1a f1 00 00 00 00 00 f0 00 00 ...............
000020 d0 cf dd e0 d1 b1 1a ee 00 00 00 50 00 d0 00 00 ...............
000030 d0 cf 11 e0 a8 b1 1a d1 00 00 00 b0 00 00 00 00 ...............
000040 eb af bd 30 41 31 11 41 00 c0 00 00 00 00 00 00 ...............
我这样写进去的查看是这样的:
00AB859CADF8CD214
楼主所看到的,是一个二进制文件,只是编辑器以16进制表示了而已。
我翻看了以下以前的代码,给楼主贴一点
自己修改一下以下代码:
'文件长度
Private myFileLen As Long
'打开的文件
Private FilePathName As String
'文件存储数组
Private BuffArray() As Byte
'将文件读入一个二进制数组
Private sub FileOpen_2 ()
Open FilePathName For Binary As #1
myFileLen = LOF(1)
ReDim BuffArray(myFileLen - 1)
Get #1, , BuffArray
Close #1
end sub'把数组写回去的代码
Open FilePathName For Binary Access Write As #1
Put #1, , BuffArray
Close #1'你可以用下面的代码把你获得的16进制转换为2进制,然后写回去看看,是不是和原来一样!
Function Hex2Bin(InputData As String) As String
On Error Resume Next
Dim I As Integer
Dim BinOut As String
Dim Lenhex As IntegerInputData = UCase(InputData)
Lenhex = Len(InputData)BinOut = ""For I = 1 To LenhexIf Mid(InputData, I, 1) = "0" Then
BinOut = BinOut + "0000"
ElseIf Mid(InputData, I, 1) = "1" Then
BinOut = BinOut + "0001"
ElseIf Mid(InputData, I, 1) = "2" Then
BinOut = BinOut + "0010"
ElseIf Mid(InputData, I, 1) = "3" Then
BinOut = BinOut + "0011"
ElseIf Mid(InputData, I, 1) = "4" Then
BinOut = BinOut + "0100"
ElseIf Mid(InputData, I, 1) = "5" Then
BinOut = BinOut + "0101"
ElseIf Mid(InputData, I, 1) = "6" Then
BinOut = BinOut + "0110"
ElseIf Mid(InputData, I, 1) = "7" Then
BinOut = BinOut + "0111"
ElseIf Mid(InputData, I, 1) = "8" Then
BinOut = BinOut + "1000"
ElseIf Mid(InputData, I, 1) = "9" Then
BinOut = BinOut + "1001"
ElseIf Mid(InputData, I, 1) = "A" Then
BinOut = BinOut + "1010"
ElseIf Mid(InputData, I, 1) = "B" Then
BinOut = BinOut + "1011"
ElseIf Mid(InputData, I, 1) = "C" Then
BinOut = BinOut + "1100"
ElseIf Mid(InputData, I, 1) = "D" Then
BinOut = BinOut + "1101"
ElseIf Mid(InputData, I, 1) = "E" Then
BinOut = BinOut + "1110"
ElseIf Mid(InputData, I, 1) = "F" Then
BinOut = BinOut + "1111"
ElseEnd IfNext IHex2Bin = BinOut
eds:
End Function哎呀,丢人了啊,呵呵,,,,,,,,以前的代码还真有初学者的风范...散了,
楼主问题的解决办法,就是把每一个字符转换为二进制,写入一个数组,然后把按上面方法写回
楼上的楼上那位大哥为什么不用 select case ....