Quote Originally Posted by Merri
VB Code:
  1. 'in a module
  2. Public Function GetByteFromFile(ByVal Filename As String, ByVal FilePosition As Long) As Byte
  3.     Dim FileNumber As Byte, Temp As Byte
  4.     FileNumber = FreeFile
  5.     On Error Goto ErrorHandler
  6.     Open Filename For Binary Access Read As #FileNumber
  7.         Seek #FileNumber, FilePosition
  8.         Get #FileNumber, , Temp
  9.     Close #FileNumber
  10.     GetByteFromFile = Temp
  11. ErrorHandler:
  12. 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:
  1. 'in a module
  2. Public Function OpenFileAsHex(ByVal Filename As String) As String
  3.     Dim ReadBuffer() As Byte, ReturnBuffer() As Byte
  4.     Dim FileNumber As Byte, A As Long, B As Long
  5.     FileNumber = FreeFile
  6.     'open file
  7.     Open Filename For Binary Access Read As #FileNumber
  8.         'prepare a read buffer
  9.         ReDim Preserve ReadBuffer(LOF(FileNumber) - 1)
  10.         'prepare a return buffer
  11.         ReDim Preserve ReturnBuffer(LOF(FileNumber) * 3 - 1)
  12.         'read all data
  13.         Get #FileNumber, , ReadBuffer
  14.     Close #FileNumber
  15.     'convert numbers to 0 - F
  16.     For A = 0 To UBound(ReturnBuffer) Step 3
  17.         'divide by three (\ is faster than /)
  18.         B = A \ 3
  19.         '&H30 = 48, which is the character code for 0
  20.         'the code before it rips the four upper bits (value will be 0 - 15)
  21.         ReturnBuffer(A) = ((ReadBuffer(B) And &HF0) \ &H10) Or &H30
  22.         'rip the four lower bits and and add 48
  23.         ReturnBuffer(A + 1) = (ReadBuffer(B) And &HF) Or &H30
  24.         'space character to divide the hex values from each other
  25.         ReturnBuffer(A + 2) = &H20
  26.         'then check if character code is bigger than the code for number 9
  27.         'and add seven if it is so 10 will be A and 11 B and so on
  28.         If ReturnBuffer(A) > 57 Then ReturnBuffer(A) = ReturnBuffer(A) + 7
  29.         If ReturnBuffer(A + 1) > 57 Then ReturnBuffer(A + 1) = ReturnBuffer(A + 1) + 7
  30.     Next A
  31.     'convert to string
  32.     OpenFileAsHex = StrConv(ReturnBuffer, vbUnicode)
  33. 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