|
-
Jun 15th, 2006, 07:54 PM
#7
Hyperactive Member
Re: Reading file as HEX
 Originally Posted by Merri
VB Code:
'in a module
Public Function GetByteFromFile(ByVal Filename As String, ByVal FilePosition As Long) As Byte
Dim FileNumber As Byte, Temp As Byte
FileNumber = FreeFile
On Error Goto ErrorHandler
Open Filename For Binary Access Read As #FileNumber
Seek #FileNumber, FilePosition
Get #FileNumber, , Temp
Close #FileNumber
GetByteFromFile = Temp
ErrorHandler:
End Function
Then you can use Hex$() to convert it to a hex. Also, I posted this code yesterday, it reads all the data in a file and shows it as hex (separated by spaces):
VB Code:
'in a module
Public Function OpenFileAsHex(ByVal Filename As String) As String
Dim ReadBuffer() As Byte, ReturnBuffer() As Byte
Dim FileNumber As Byte, A As Long, B As Long
FileNumber = FreeFile
'open file
Open Filename For Binary Access Read As #FileNumber
'prepare a read buffer
ReDim Preserve ReadBuffer(LOF(FileNumber) - 1)
'prepare a return buffer
ReDim Preserve ReturnBuffer(LOF(FileNumber) * 3 - 1)
'read all data
Get #FileNumber, , ReadBuffer
Close #FileNumber
'convert numbers to 0 - F
For A = 0 To UBound(ReturnBuffer) Step 3
'divide by three (\ is faster than /)
B = A \ 3
'&H30 = 48, which is the character code for 0
'the code before it rips the four upper bits (value will be 0 - 15)
ReturnBuffer(A) = ((ReadBuffer(B) And &HF0) \ &H10) Or &H30
'rip the four lower bits and and add 48
ReturnBuffer(A + 1) = (ReadBuffer(B) And &HF) Or &H30
'space character to divide the hex values from each other
ReturnBuffer(A + 2) = &H20
'then check if character code is bigger than the code for number 9
'and add seven if it is so 10 will be A and 11 B and so on
If ReturnBuffer(A) > 57 Then ReturnBuffer(A) = ReturnBuffer(A) + 7
If ReturnBuffer(A + 1) > 57 Then ReturnBuffer(A + 1) = ReturnBuffer(A + 1) + 7
Next A
'convert to string
OpenFileAsHex = StrConv(ReturnBuffer, vbUnicode)
End Function
Usage:
Text1.Text = OpenFileAsHex("c:\autoexec.bat")
If you want to get a specific hex from the string, you can look it with logic Mid$(Text1.Text, Position * 3, 2). The code is rather fast once you compile it 
Great code
But, can U make it afther load hex file in text box, save it again to binary ???
Usage:
Text1.Text = SaveFileAsBinary("c:\autoexec.bat")
B.R
VB Client/Server
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|