hex editing in visual studio

This topic is 4732 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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))
{
{
}
}
}
}
}


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))
{
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);
for (int i = 0; i < 11; i++)
{
}
w.Close(); //************Closed again for no reason.  fs and r are not closed***************
}
}



Share on other sites
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 on other sites
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 on other sites
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 on other sites
Haha, that was it! [smile]

Thanks for the help! If only I knew why my file won't open like that from a windows folder, when the msdn example does.

Share on other sites
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 on other sites
It is the example that I posted. I used the exact same file name/extension as the example, too...

Share on other sites
Hmm... that makes it doubly bizarre then...

Share on other sites
It might scan the file to try and determine if it should open it as a binary or text.

Share on other sites
Quote:
 Original post by Nemesis2k2It 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.

• 9
• 9
• 13
• 41
• 15