Thread: C# and hex

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    1

    C# and hex

    Hi currently I'm working on a project and I'm wondering if anyone can help me, I have been set a task of creating a editor which I have done yes its basic and yes its from a book but I would like to add one more thing to it but I'm stuffed and cannot seem to get it to work.

    basically I would like the output to be in hex rather than plain text, is this possible?

    the code is
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing.Printing;
    using System.Collections;
    using System.Windows.Forms;
    
    namespace Simple_ed
    {
        public partial class frmMain : Form
        {
            private string m_sfileName = "";
            public frmMain()
            {
                InitializeComponent();
            }
    
            private void clearFormText()
            {
                this.Text = "Simple Editor - [unnamed]";
            }
    
            private void mnuFileExit_Click(object sender, System.EventArgs e)
            {
                // exit the application but query user if the text in the box has changed
                if (rtfMain.Modified == true)
                {
                    if (MessageBox.Show("Abandon all changes? - All data will be LOST", "Confirm exit",
                    MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        Application.Exit();
                    }
                }
                else
                {
                    Application.Exit();
                }
            }
    
            private void mnuFileNew_Click(object sender, EventArgs e)
            {
                // create new document with a check to see if the document has been altered
                if (rtfMain.Modified == true)
                {
                    if (MessageBox.Show("Create new document? - All unsaved work will be LOST?", "New Document",
                    MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        rtfMain.Clear();
                        m_sfileName = ""; // clear the file name
                        clearFormText(); // and reset the form caption
                    }
                }
                else
                {
                    rtfMain.Clear();
                    m_sfileName = ""; // clear the file name
                    clearFormText(); // and reset the form caption
                }
            }
    
            private void setFormText(string sFile)
            {
                // create an instance of a FileInfo class and use
                // it to get the file's name without its path
                System.IO.FileInfo fInfo;
                fInfo = new System.IO.FileInfo(sFile);
                this.Text = "Simple Editor - [" + fInfo.Name + "]";
            }
    
            private void mnuFileOpen_Click(object sender, EventArgs e)
            {
                bool bDoOpen = true;
                // show an Open File dialog and open the file, but
                // check that the current text has not changed first
                if (rtfMain.Modified == true)
                {
                    if (MessageBox.Show("Text has changed - really open another document?",
                    "Confirm file open", MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question) == DialogResult.No)
                        bDoOpen = false;
                }
                if (bDoOpen == true)
                {
                    // get a file to open and open it
                    dlgOpen.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
                    dlgOpen.FilterIndex = 1;
                    if (dlgOpen.ShowDialog() == DialogResult.OK && dlgOpen.FileName.Length > 0)
                    {
                        rtfMain.LoadFile(dlgOpen.FileName, RichTextBoxStreamType.PlainText);
                        rtfMain.Modified = false;
                        m_sfileName = dlgOpen.FileName;
                        setFormText(m_sfileName);
                    }
                }
            }
    
            private void mnuFileSave_Click(object sender, EventArgs e)
            {
                // save the file if we have a filename for it
                if (m_sfileName != "")
                    rtfMain.SaveFile(m_sfileName, RichTextBoxStreamType.PlainText);
                else
                    mnuFileSaveAs_Click(sender, e);
            }
    
            private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
            {
    
            }
    
            private void mnuFileSaveAs_Click(object sender, EventArgs e)
            {
                // if we have a filename, use that in the dialog, otherwise use
                // a generic name
                if (m_sfileName != "")
                    dlgSave.FileName = m_sfileName;
                else
                    dlgSave.FileName = "SimpleEd1";
                dlgSave.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
                dlgSave.FilterIndex = 1;
                dlgSave.DefaultExt = "txt";
                if (dlgSave.ShowDialog() == DialogResult.OK && dlgSave.FileName.Length > 0)
                {
                    rtfMain.SaveFile(dlgSave.FileName,
                    RichTextBoxStreamType.PlainText);
                    rtfMain.Modified = false;
                    m_sfileName = dlgSave.FileName;
                    setFormText(m_sfileName);
                }
            }
        }
    }
    In my simplistic approach I am guessing that it is something to do with this line
    Code:
    rtfMain.LoadFile(dlgOpen.FileName, RichTextBoxStreamType.PlainText);
    but as I have found learning this language nothing is as simple as it seems.

    its just a starting point for me to learn really and i will be expanding it as much as i can as i learn ATM I'm just learning how to do the cut copy and paste.

    I thank anyone in advance for the help they may offer

    Sorry I forgot to say I'm using Microsoft visual studio 2010 c# if that makes a difference.
    Last edited by RampantRabbit; 05-30-2010 at 12:26 AM. Reason: Missed out something

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    One way you can do it is after you've called rtfMain.LoadFile(), convert the text in rtfMain to hex.

    I found a function online that converts a string to hex:
    C# - Convert ASCII String To Hex | testingReflections.com

    Here's the function on that page if you can't see it for whatever reason:
    Code:
    public string ConvertToHex(string asciiString)
    {
        string hex = "";
        foreach (char c in asciiString)
        {
            int tmp = c;
            hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
        }
        return hex;
    }

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Incrementing strings together is a terrible idea in C#. They're just not made to work that way. Instead, you should use a StringBuilder.
    Code:
    public static string ConvertToHex(string asciiString)
    {
        StringBuilder hex = new StringBuilder();
    
        foreach (char c in asciiString)
            hex.AppendFormat("{0:x2}", (uint)c);
    
        return hex.ToString();
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed