View a file in Hex

G

Guest

I have a binary file and I want to view it completely in hex.
I tried it with following code...:


Private Function getHex(ByVal str As String) As String
Dim enc As New UTF8Encoding
Dim output As New StringBuilder

For Each b As Byte In enc.GetBytes("D:\test.rsc")
output.Append(Hex(b))
Next

Return output.ToString
End Function


but if I compare the result with a hexviewer, the values are different. On
some places there are some more zeros and on others there are not enough of
them.
For example it should look like "04F0050045" but it looks like "40F5045".
I tried changing the encofing already but without any success.
Thank you in advance for helping me!
 
M

Mattias Sjögren

For Each b As Byte In enc.GetBytes("D:\test.rsc")

Encoding.GetBytes returns the bytes of the string you pass in
converted to the given encoding. It doesn't open the file if you
specify a file path, you have to do that yourself.

Using f As New FileStream("D:\test.rsc", FileMode.Open)
Dim b As Integer = f.ReadByte()
Do While b >= 0
output.AppendFormat("X2", b)
b = f.ReadByte()
Loop
End Using


Mattias
 
G

Guest

Well, with your solution it only displays X2 for wach read out byte.
With ' output.Append(Hex(b)) ' there are still less zeros
 
B

Branco Medeiros

kenny said:
I have a binary file and I want to view it completely in hex.
I tried it with following code...:
For Each b As Byte In enc.GetBytes("D:\test.rsc")
output.Append(Hex(b))
Next
but if I compare the result with a hexviewer, the values are different. On
some places there are some more zeros and on others there are not enough of
them.
For example it should look like "04F0050045" but it looks like "40F5045".
<snip>

The Hex function doesn't left pad with zeroes. For instance, it will
return "C" and not "0C" for Hex(12). You must pad it yourself:

output.append(b.ToString("X2"))

Regards,

Branco.
 
G

Guest

yes, I noticed that the Hex function doesn't return any zeros.
now I understand. It works fine and every 0 is on its place.

Thank you very much!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top