Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C# Programming

C# Programming C# (pronounced C-sharp) is a new object oriented language from Microsoft and is derived from C and C++. It also borrows a lot of concepts from Java too including garbage collection.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #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
                }
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 04-06-2008, 07:43 AM
Xav's Avatar   
Xav Xav is offline
Guru
 
Join Date: Mar 2008
Location: London, England
Posts: 3,021
Last Blog:
I thought about progra...
Rep Power: 26
Xav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to all
Send a message via MSN to Xav
Default 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
__________________
Xav, the power of youth
Worship the Creator... not his creations
Web Site | Beta Site

Last edited by Xav; 04-06-2008 at 07:44 AM. Reason: Spelling
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-06-2008, 07:46 AM
Xav's Avatar   
Xav Xav is offline
Guru
 
Join Date: Mar 2008
Location: London, England
Posts: 3,021
Last Blog:
I thought about progra...
Rep Power: 26
Xav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to all
Send a message via MSN to Xav
Default 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.
__________________
Xav, the power of youth
Worship the Creator... not his creations
Web Site | Beta Site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
i have a problem please help me!!!???? stack Java Help 8 09-22-2007 03:17 PM
[C] Comparison problem Alhazred C and C++ 1 08-29-2007 04:58 AM
problem with boot gwo Computer Hardware 6 07-18-2007 03:15 AM
A small problem in the output The_Master C and C++ 3 12-13-2006 12:04 PM


All times are GMT -5. The time now is 10:39 AM.

Contest Stats

dargueta ........ 93.00000
John ........ 87.50000
Xav ........ 50.00000
MeTh0Dz ........ 20.00000
gaylo565 ........ 18.00000
Johnnyboy ........ 3.00000

Contest Rules

Ads