Hello, my name is Chris and I am writing a C# Application for my employer. I will not go into the nitty gritty of what it's for, but basically I am dumping and manipulating the hexadecimal of various files. The program at its core function is working, but I am finding it difficult to proceed from here. Part of the problem is that I am at heart more of a C programmer, but I seem to be picking up this managed code thing decently well. My dev environment is Visual C#.
Problem 1: I need to space the hex dump to the textbox by sets of 2, and have the hex in lines of 16. This needs to line up with and match the ASCII dump.
Problem 2: When I highlight something in one textbox, it needs to highlight the equivalent in the other. Example: If I highlight the ASCII of the word "System", it needs to highlight the equivalent in the Hexadecimal dump textbox
Problem 3: When I open a non-textfile it only seems to dump the first byte or so. Is it stopping on a null byte or what? Why does it only do a full ASCII dump when there are only ASCII valid bytes? Why do binaries not dump in ASCII properly, but they do in hex?
Code:long filesize; //declared some utility variables txtHex.Text = ""; txtASCII.Text = ""; //blanked the textbox OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "All Files|*.*"; ofd.ShowDialog(); //opened file from openfiledialog for reading try { FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read, FileShare.Read); filesize = fs.Length; byte[] bytes = new byte[filesize]; BinaryReader br = new BinaryReader(fs); bytes = br.ReadBytes((int)filesize); label2.Text = Convert.ToString(bytes.Length) + " bytes"; StringBuilder stringb = new StringBuilder(); for (int i = 0; i < bytes.Length; i++) { stringb.Append(bytes[i].ToString("X2")); } txtHex.Text = stringb.ToString(); txtASCII.Text = System.Text.ASCIIEncoding.ASCII.GetString(bytes); //EUREKA IT WORKS //HEX STRING REPORTING FOR DUTY, SIR! //ASCII REPORTING AND WORKING } catch (Exception) { return; // this is to solve an error where it would crash if you hit cancel }
I don't know much about hexadecimal bytes, and so Problems 1 and 3 I cannot help you with. However, Problem 2 is a general programming task, and it shouldn't be much of a hassle.
You need to load up the event that occurs whenever the user selects some text. This may be something like SelectionChanged(), I can't remember the details off the top of my head!
In this, here's what you do:
1. Transfer the caret position across using textbox.SelectionStart(). This returns the index of the cursor in the textbox.
2. Transfer the number of selected characters across using textbox.SelectionLength(). This returns the length of the selection.
Here's the code for the txtASCII_SelectionChanged() event:
In the txtHex_SelectionChanged() event, do the opposite, assigning the Selection values to the txtASCII textbox. This way, the selection will always be synchronised. You might want a try/catch block in case there is an error or something.Code:txtHex.SelectionStart = txtASCII.SelectionStart; //Place cursor in right place. txtHex.SelectionLength = txtASCII.SelectionLength; //Select correct number of characters. txtHex.ScrollToCaret(); //If there is a lot of text, scroll the textbox to where the selection is.
Xav
Last edited by Xav; 04-06-2008 at 05:44 AM. Reason: Spelling
I've just had a thought. Text files are usually encoded in ASCII, but files do not have to be encoded using them. It could be encoded as Unicode or UTF-8 instead. Maybe that's why they don't dump properly, because they're not encoded in the right format?
Sorry, I don't really know much about hexadecimal dumps and all that.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks