Option Explicit
Private Sub Command1_Click()
Dim b() As Byte
'读文件
Open "z:\11.bmp" For Binary As #1
ReDim b(LOF(1) - 1)
Get #1, , b
Close #1
'专成txt
Dim i As Long
Dim strTmp As String
For i = LBound(b) To UBound(b)
DoEvents
strTmp = strTmp & Format(Hex(b(i)), "00") & " "
Next
'再转成二进制数据保存
ReDim b(Len(strTmp) / 3 - 1)
For i = 0 To Len(strTmp) / 3 - 1
DoEvents
b(i) = Val("&H" & Mid(strTmp, i * 3 + 1, 2))
Next
Open "z:\22.bmp" For Binary As #2
Put #2, , b
Close #2
End Sub
Private Function FileToHexStr(ByVal strFileName As String, Optional blockStartPos As Long = 1, Optional blockLen As Long = 131072, Optional bDebug As Boolean = False) As String
Dim strReturn As String
Dim i As Long
Dim lpBuffer() As Byte
strReturn = ""
On Error GoTo ErrProc:
Open strFileName For Binary As #1
If LOF(1) = 0 Then
Close #1
Exit Function
End If
If blockStartPos <= 0 Or blockStartPos > LOF(1) Or blockLen <= 0 Then
Close #1
Exit Function
End If
If LOF(1) < blockLen + blockStartPos - 1 Then
blockLen = LOF(1) - blockStartPos + 1
End If
ReDim lpBuffer(blockLen - 1) As Byte
Get 1, blockStartPos, lpBuffer
Close #1
If bDebug Then
strReturn = "$$$Debug info - " & "File: " & strFileName
For i = 0 To UBound(lpBuffer)
If i Mod 16 = 0 Then
strReturn = strReturn & vbCr + vbLf & FixString(Hex(i), 8, "0") & "H "
End If
strReturn = strReturn & " " & IIf(CLng(lpBuffer(i)) > 15, Hex(lpBuffer(i)), "0" & Hex(lpBuffer(i)))
Next
Else
Dim sb As New clsCFXStrBuilder
For i = 0 To UBound(lpBuffer)
sb.Append IIf(CLng(lpBuffer(i)) > 15, Hex(lpBuffer(i)), "0" & Hex(lpBuffer(i)))
Next
FileToHexStr = sb.FullString
Exit Function
End If
FileToHexStr = strReturn
Exit Function
ErrProc:
FileToHexStr = ""
Debug.Print Err.Description
End Function
Private Function FixString(ByVal strSrc As String, ByVal nLen As String, ByVal strFill As String) As String
FixString = String(nLen - Len(strSrc), strFill) & strSrc
End Function
Private Sub HexStrToFile(ByVal strFileName As String, ByVal strContent As String, Optional ByVal BlockPos As Long = 1)
Dim lpBuffer() As Byte
Dim i As Long
On Error GoTo ErrProc:
If BlockPos < 1 Then
Exit Sub
End If
If Len(strContent) Mod 2 <> 0 Then
Exit Sub
End If
If BlockPos = 1 Then
Open strFileName For Output As #1
Close #1
End If
If strContent = "" Then
Exit Sub
End If
ReDim lpBuffer(Len(strContent) / 2 - 1)
For i = 0 To UBound(lpBuffer)
lpBuffer(i) = Val("&H" & Mid(strContent, i * 2 + 1, 2))
Next
Open strFileName For Binary As #1
Put 1, BlockPos, lpBuffer
Close #1
Exit Sub
ErrProc:
Debug.Print Err.Description
End Sub
Option Explicit
Private m_content As String
Private m_buffer As String
Private m_sbuffer As String
Private m_bufferlen As Long
Private m_sbufferlen As Long
Public Sub Append(ByVal str As String)
m_sbuffer = m_sbuffer & str
m_sbufferlen = m_sbufferlen + 1
If m_sbufferlen > 128 Then
m_buffer = m_buffer & m_sbuffer
m_bufferlen = m_bufferlen + 1
m_sbuffer = ""
m_sbufferlen = 0
End If
If m_bufferlen > 128 Then
m_content = m_content & m_buffer
m_buffer = ""
m_bufferlen = 0
End If
End Sub
Public Property Get FullString() As String
FullString = m_content & m_buffer & m_sbuffer
End Property
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。