Closed Thread
Results 1 to 3 of 3

Thread: Peculiar UI Problem Needs Tackling

  1. #1
    adriyel Guest

    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
                    }

  2. CODECALL Circuit advertisement

     
  3. #2
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Peculiar UI Problem Needs Tackling

    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:

    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.
    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.

    Xav
    Last edited by Xav; 04-06-2008 at 05:44 AM. Reason: Spelling

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  4. #3
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Peculiar UI Problem Needs Tackling

    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.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. If problem or cout problem?
    By chaoticape in forum C and C++
    Replies: 4
    Last Post: 06-10-2011, 10:29 AM
  2. C: Problem with solving problem
    By rakche in forum C and C++
    Replies: 15
    Last Post: 03-28-2010, 01:24 PM
  3. Replies: 0
    Last Post: 04-26-2007, 05:33 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts