|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
View a file in HexI 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 > For Each b As Byte In enc.GetBytes("D:\test.rsc") Encoding.GetBytes returns the bytes of the string you pass inconverted 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. 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. > kenny wrote:
> I have a binary file and I want to view it completely in hex. <snip>> I tried it with following code...: > For Each b As Byte In enc.GetBytes("D:\test.rsc") <snip>> output.Append(Hex(b)) > Next > but if I compare the result with a hexviewer, the values are different. On <snip>> 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". 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. 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. > >
Option Strict On
disabling controls by checking off a radio button when the form loads Currently logged in user's email Format function... Confused! Check Interface Implementation of a Type SENDMail Get a simple handler Invoking CTRL+C,X,V programatically Double-Buffering in VB2005? save string var to html file without losing format |
|||||||||||||||||||||||