By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,061 Members | 1,539 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,061 IT Pros & Developers. It's quick & easy.

View a file in Hex

P: n/a
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!
----
k3nny
Apr 1 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
> 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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Apr 1 '06 #2

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

"Mattias Sjögren" wrote:
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Apr 1 '06 #3

P: n/a

kenny wrote:
I have a binary file and I want to view it completely in hex.
I tried it with following code...: <snip> For Each b As Byte In enc.GetBytes("D:\test.rsc")
output.Append(Hex(b))
Next <snip>
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.

Apr 1 '06 #4

P: n/a
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!!
----
k3nny


"Branco Medeiros" wrote:

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.

Apr 1 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.