将文件读取为16禁止

鸭梨山大帝 2008-03-11 02:26:48
我有一个很小的任意类型的文件,我希望办到以下的几点:
1.读取这个文件十六制内容,然后保存为如:"30 82 00 FF 0C 0D" 这种字串到TXT
2.读取这个TXT中的如:"30 82 00 FF 0C 0D"的字串,还原出这个完整的文件保存到硬盘
如何能办到,希望麻烦各位高手们给出思路方法
如果不能办到,希望麻烦解释一下为什么无法办到
...全文
51 点赞 收藏 8
写回复
8 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
鸭梨山大帝 2008-03-11
谢了~,高人,结贴给分
回复
Option Explicit

Private Sub Command1_Click()
binary2ascii "d:\0.jpg", "d:\1.txt"
ascii2binary "d:\1.txt", "d:\1.jpg"
End Sub

Private Function gethex(ByVal c As Byte) As String
Dim s As String
s = Trim(Hex$(c))
If c < 16 Then
s = "0" & s
End If
gethex = s
End Function
Private Function h2s(ByRef h() As Byte, ByVal start As Long, ByVal length As Long) As String
Dim s As String
Dim i As Long
For i = start To start + length - 1
s = s & gethex(h(i)) & " "
Next i
h2s = s
End Function

Private Function binary2ascii(ByVal src As String, ByVal dest As String) As Boolean
Dim len1 As Long
Dim bin() As Byte
len1 = FileLen(src)
ReDim bin(0 To len1 - 1) As Byte
Open src For Binary As #1
Get #1, , bin
Close #1
Dim s As String

Dim i As Long
Open dest For Output As #1
For i = 0 To len1 - 1 Step 16
If len1 - 1 - i > 16 Then
s = h2s(bin, i, 16)
Else
s = h2s(bin, i, len1 - i)
End If
Print #1, s
Next i
Close #1

binary2ascii = True
End Function

Private Function parse(ByVal s As String, ByRef bin() As Byte, ByVal offset As Long) As Long
s = Replace(s, " ", "")
Dim i As Long
Dim l As Long
For i = 1 To Len(s) Step 2
bin(offset + l) = Val("&h" & Mid(s, i, 2))
l = l + 1
Next i
parse = l
End Function
Private Function ascii2binary(ByVal src As String, ByVal dest As String) As Boolean
Dim s As String
Dim len1 As Long
Dim parselen As Long
Dim bin(0 To 15) As Byte
Dim i As Long

Open dest For Binary As #2
Close #2
Kill dest

Open src For Input As #1
Open dest For Binary As #2
Do
Line Input #1, s
len1 = parse(s, bin, 0)
If 16 = len1 Then
Put #2, , bin
Else
For i = 0 To len1 - 1
Put #2, , bin(i)
Next i
End If
Loop While Not EOF(1)
Close #2
Close #1
ascii2binary = True
End Function
回复
鸭梨山大帝 2008-03-11
谢谢,读取已经OK,但是目前 如何把这个TXT字串 还原成之前的文件呢(假定,我使用的是A.EXE文件为源)
回复
Option Explicit

Private Sub Command1_Click()
binary2ascii "d:\1.bin", "d:\1.txt"
End Sub

Private Function gethex(ByVal c As Byte) As String
Dim s As String
s = Trim(Hex$(c))
If c < 16 Then
s = "0" & s
End If
gethex = s
End Function
Private Function h2s(ByRef h() As Byte, ByVal start As Long, ByVal length As Long) As String
Dim s As String
Dim i As Long
For i = start To start + length - 1
s = s & gethex(h(i)) & " "
Next i
h2s = s
End Function

Private Function binary2ascii(ByVal src As String, ByVal dest As String) As Boolean
Dim len1 As Long
Dim bin() As Byte
len1 = FileLen(src)
ReDim bin(0 To len1 - 1) As Byte
Open src For Binary As #1
Get #1, , bin
Close #1
Dim s As String

Dim i As Long
Open dest For Output As #1
For i = 0 To len1 - 1 Step 16
If len1 - 1 - i > 16 Then
s = h2s(bin, i, 16)
Else
s = h2s(bin, i, len1 - i)
End If
Print #1, s
Next i
Close #1

binary2ascii = True
End Function
回复
CommandButton 2008-03-11
当然能办到,否则比尔盖茨会很伤心的。等1楼的代码吧
回复
鸭梨山大帝 2008-03-11
已知的任意类型,以EXE执行档为例来举例看看?
回复
舉杯邀明月 2008-03-11
  读数据文件:

  定义一 Byte 类型的变量bData,用二进制方式打开文件进行读操作,每次就可以只读一个字节了。
  定义一 String 类型的变量sOutStr,用
sOutStr = Right$("0" & Hex$(bData) & " ", 3)
  把每一个字节的数据转换成十六进制的字符串(加1空格),再把它依次输出到另一个用二进制方式打开文件中,就转换成文本文件了。
  输出时用这样的语句(假设输出文件号是2):
Print #2, , sOutStr;
  注意后面有分号,否则它会加上“回车”、“换行”两个控制字符。



LZ没有说清楚文本文件的具体存放格式,因此不好说“还原”的具体细节。
回复
这很简单,一会儿给你一个程序。
回复
相关推荐
发帖
VB基础类
创建于2007-09-28

7492

社区成员

VB 基础类
申请成为版主
帖子事件
创建了帖子
2008-03-11 02:26
社区公告
暂无公告