|
||||||
| 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. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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
}
|
| Sponsored Links |
|
|
|
|||||
|
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. 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 |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
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 |