Home All Groups Group Topic Archive Search About
Author
1 Apr 2006 9:28 AM
kenny
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

Author
1 Apr 2006 11:59 AM
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

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
1 Apr 2006 1:56 PM
kenny
Well, with your solution it only displays X2 for wach read out byte.
With  ' output.Append(Hex(b)) ' there are still less zeros

Show quoteHide quote
"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.
>
Author
1 Apr 2006 2:53 PM
Branco Medeiros
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.
Author
1 Apr 2006 3:11 PM
kenny
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


Show quoteHide quote
>
>"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.
>
>