View Single Post
  #1 (permalink)  
Old 01-12-2007, 01:28 PM
adriyel adriyel is offline
Newbie
 
Join Date: Jan 2007
Posts: 1
Rep Power: 0
adriyel is on a distinguished road
Default Peculiar UI Problem Needs Tackling

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
                }
Reply With Quote

Sponsored Links