From: Bert Nelsen on
Hello,
can anybody please tell me how to read the hex values of a binary file?
I have a procedure but it shows different values than a commerical hex
viewer, and I don't know who is wrong...
Or if anybody can tell me if what I do below is correct, I'd be happy
already.


sTempDataStorage = String(1024, " ")
'sets the length of sHex = to 2 characters wide. This now means
that sHex is now a fixed length string
sHex = String(2, " ")

'Opens the chosen file for binary access
Open uPath For Binary Access Read As #iFileNr

'Sets the amount of loops it will take to load the open file in 1kb
chunks.
'So if the loops variable was set to 1024 times, then the file
would be 1mb in size.

Dim lFileLen&
lFileLen = Int(FileLen(uPath) / 1024) + 1

Dim m& 'long integer to loop though each 1kb chunk from data got
from the open file
'loop to go through the each kb of data retrieved from the file and
parse it
For m = 1 To lFileLen

If m_bCancel Then
GoTo Jumper
End If

DoEvents

Get #iFileNr, , sTempDataStorage

strTemp = sTempDataStorage
'loop that goes through each byte of data in the returned string
'and converts it to its hex equivelent and stores it in an array
For l = 1 To Len(strTemp)

Dim iByte%
Dim sByte$

sByte = Mid(strTemp, l, 1) 'gets a byte at a time
iByte = Asc(sByte) 'converts the current byte to its ascii
value
sHex = LCase(Hex(iByte)) 'converts the ascii value of the
current
From: Jeff Johnson on
"Bert Nelsen" <bert.nelsen(a)googlemail.com> wrote in message
news:%236yxlhu0IHA.2384(a)TK2MSFTNGP02.phx.gbl...

Pretty sure your main problem is right here:

> Get #iFileNr, , sTempDataStorage

Use a byte array instead of a string.


From: Saga on
Try this:

Start a new standard project, drop a label, text, button and list control
into the form. Use default properties and names. Now drop this code
in:

Option Explicit

Private Sub Command1_Click()

Dim nFile As Long
Dim TotalFile As String
Dim i As Long
Dim strHex As String
Dim strPath As String

'Assume a valid, existing file.
strPath = Text1.Text

'Load file into TotalFile variable.
nFile = FreeFile
Open strPath For Binary As #nFile
TotalFile = Space(LOF(nFile))
Get #nFile, , TotalFile
Close #nFile

For i = 1 To Len(TotalFile)
If i Mod 16 = 0 Then
'Display and reinit line.
List1.AddItem Left$(strHex, Len(strHex) - 1)
strHex = ""
End If

'Get hex char.
strHex = strHex & Right$("0" & Hex$(Asc(Mid$(TotalFile, i, 1))), 2)
strHex = strHex & "-" 'Hex value separator.
Next i

'Display last line.
List1.AddItem Left$(strHex, Len(strHex) - 1)

End Sub

Private Sub Form_Load()

Text1.Text = ""
Label1.Caption = "Filename:"
Command1.Caption = "Display hex"

End Sub

Run project and type in a valid file name into the text box.

This example follows your idea of using a string variable. Take note
of Jeff's idea.

Saga
--




"Bert Nelsen" <bert.nelsenNON(a)SPAMMEgooglemail.com> wrote in message
news:%236yxlhu0IHA.2384(a)TK2MSFTNGP02.phx.gbl...
> Hello,
> can anybody please tell me how to read the hex values of a binary file?
> I have a procedure but it shows different values than a commerical hex viewer, and I don't know
> who is wrong...
> Or if anybody can tell me if what I do below is correct, I'd be happy already.
>
>
> sTempDataStorage = String(1024, " ")
> 'sets the length of sHex = to 2 characters wide. This now means that sHex is now a fixed
> length string
> sHex = String(2, " ")
>
> 'Opens the chosen file for binary access
> Open uPath For Binary Access Read As #iFileNr
>
> 'Sets the amount of loops it will take to load the open file in 1kb chunks.
> 'So if the loops variable was set to 1024 times, then the file would be 1mb in size.
>
> Dim lFileLen&
> lFileLen = Int(FileLen(uPath) / 1024) + 1
>
> Dim m& 'long integer to loop though each 1kb chunk from data got from the open file
> 'loop to go through the each kb of data retrieved from the file and parse it
> For m = 1 To lFileLen
>
> If m_bCancel Then
> GoTo Jumper
> End If
>
> DoEvents
>
> Get #iFileNr, , sTempDataStorage
>
> strTemp = sTempDataStorage
> 'loop that goes through each byte of data in the returned string
> 'and converts it to its hex equivelent and stores it in an array
> For l = 1 To Len(strTemp)
>
> Dim iByte%
> Dim sByte$
>
> sByte = Mid(strTemp, l, 1) 'gets a byte at a time
> iByte = Asc(sByte) 'converts the current byte to its ascii value
> sHex = LCase(Hex(iByte)) 'converts the ascii value of the current


From: Bert Nelsen on
Thank you!!
From: Mike Williams on
"Bert Nelsen" <bert.nelsen(a)googlemail.com> wrote in message
news:%236yxlhu0IHA.2384(a)TK2MSFTNGP02.phx.gbl...

> can anybody please tell me how to read the hex values of
> a binary file? I have a procedure but it shows different values
> than a commerical hex viewer, and I don't know who is wrong...

You are :-)

As Geoff has said, you will find this very much easier using a Byte array
and loading the entire file into the Byte aray in one gulp (if the file is
not much larger than a few tens of megabytes) or in a number of separate
"gulps" if it is very much larger. You can then display the data in any way
you wish.

Mike