Sign in to follow this  
Daniel Miller

hex editing in visual studio

Recommended Posts

Daniel Miller    218
I can't get my binary file to be opened in hex format by VS. The MSDN example opens fine, but mine appears as 4 "y"s with 2 dots above each. I tried right-clicking -> edit, but it still won't work. Any ideas? Here is the code to save the file:
using System;
using System.IO;

namespace BinaryFileIO
{
	public class MainClass
	{
		public static void Main()
		{
			string fileName = "test.data";
			using(FileStream fileStream = new FileStream (fileName, FileMode.Create))
			{
				using (BinaryWriter fileWriter = new BinaryWriter(fileStream))
				{
					fileWriter.Write((byte)System.Byte.MaxValue);
					fileWriter.Write((byte)System.Byte.MaxValue);
					fileWriter.Write((byte)System.Byte.MaxValue);
					fileWriter.Write((byte)System.Byte.MaxValue);
				}
			}
			using(FileStream fileStream = new FileStream (fileName, FileMode.Open, FileAccess.Read))
			{
				using (BinaryReader fileReader = new BinaryReader(fileStream))
				{
					Console.WriteLine(fileReader.ReadByte());
					Console.WriteLine(fileReader.ReadByte());
					Console.WriteLine(fileReader.ReadByte());
					Console.WriteLine(fileReader.ReadByte());
				}
			}			
		}
	}
}

I noticed something about the MSDN example. They close the binary writer twice for no reason, and they don't close filestream and binary reader after they have been opened:
using System;
using System.IO;
class MyStream 
{
    private const string FILE_NAME = "Test.data";
    public static void Main(String[] args) 
    {
        // Create the new, empty data file.
        if (File.Exists(FILE_NAME)) 
        {
            Console.WriteLine("{0} already exists!", FILE_NAME);
            return;
        }
        FileStream fs = new FileStream(FILE_NAME, FileMode.CreateNew);
        // Create the writer for data.
        BinaryWriter w = new BinaryWriter(fs);
        // Write data to Test.data.
        for (int i = 0; i < 11; i++) 
        {
            w.Write( (int) i);
        }
        w.Close();//*******************Closed once************************
        fs.Close();
        // Create the reader for data.
        fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
        BinaryReader r = new BinaryReader(fs);
        // Read data from Test.data.
        for (int i = 0; i < 11; i++) 
        {
            Console.WriteLine(r.ReadInt32());
        }
        w.Close(); //************Closed again for no reason.  fs and r are not closed***************
    }
}





Share this post


Link to post
Share on other sites
ApochPiQ    23065
In VS6: File->Open->Select file->Open as type "binary"->Open. This will get you into hex-editing mode. I'm sure there is an analogous process in VS.Net but I can't remember it off the top of my head, as I haven't been using VS.Net for very long.

Share this post


Link to post
Share on other sites
Daniel Miller    218
Thanks [smile], but I looked around for what you described (or something similar), but I couldn't find it. The strange thing is that the MSDN's example opens fine.

Share this post


Link to post
Share on other sites
moeron    326
in VC++ .NET 2003 you go to File->Open->File and you click on the little upside-down triangle thing next to the open button, then click "open with" to open stuff in various modes. I only see binary on mine. But the debug memory tools allow hex, I don't know why they don't have stuff like that for files too..

Share this post


Link to post
Share on other sites
ApochPiQ    23065
Depending on the file type of the MSDN sample, it might be set up in the registry to automatically open in binary mode with VS. I'm not familiar with the specific sample you're referring to, though, so that's pure speculation.

Share this post


Link to post
Share on other sites
iMalc    2466
Quote:
Original post by Nemesis2k2
It might scan the file to try and determine if it should open it as a binary or text.
Yes, it does. It indeed has nothing to do with the file type.
I'm not sure how much of the contents it looks at, but if it thinks that the bit if looks at could conceivably be text, then it wont open it in the binary editor.
Also, if you ask me, it's method of deciding whether to open it in the binary editor or not is utter crap and often gets it wrong.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this