Thread: C# problem

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    7

    C# problem

    Does anyone know how to store the contents of a file then read to a string array, then convert it to hex, then read it to a richtextbox and then read to a second string and convert it back to text and save to the original file?

    the file is opened through the openfiledialog method; where I then browse for the file (using a filter) select and the open i.e -:

    Code:
                     OpenFileDialog dialog = new OpenFileDialog();
                    dialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                    dialog.InitialDirectory = "@c:\\";
                    dialog.Title = "Select a text file";
    I tried using this :

    Code:
                    StreamReader streamReader = new StreamReader(OpenFileDialog.dialog);
                    string text = streamReader.ReadToEnd();
                    streamReader.Close();
    to read the file to a string, But the variable after new StreamReader(.... ) I can't quite get. Any help would be much appreciated.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I'm to tired to even remotely understand that cryptic question, however to read the contents from a file to a string there is a very useful wrapper function:
    Code:
    System.Windows.Forms.OpenFileDialog FileDialog = new System.Windows.Forms.OpenFileDialog();
    FileDialog.Filter = ""; //Enter filter here
    
    if(FileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
      string Text = System.IO.File.ReadAllText(FileDialog.FileName);
    }
    There's also a similar ReadAllLines function if you prefer to get a string[], one string per line.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    7

    Thanks

    Thankyou Magos, The code has helped me and has got rid of much of the garbage I had in before! Now, using that code, where the file is now in the string, how can I now convert that to hex? and then load the converted data into the richtextbox? Sorry if that doesn't make much sense

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    What do you mean "to hex"?
    To enter text into a richtextbox, simply:
    Code:
    MyRichTextBox.Text = "Hi there!";
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    7
    Heaxadecimal, the text file containts only A-Z characters, which I want to convert to hexadecimal format. Then ouput the new string text to the richtextbox.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I'm sure you could traverse the string character by character and convert them one by one. Doesn't sound too effective, I'm sure there are better ways.
    Code:
    string Text = "Yatta!";
    string HexText = "";
    char[] HexTable = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
    
    foreach(char Character in Text.ToCharArray())
    {
    	int HexValue = (int)Character;
    	HexText += HexTable[HexValue / 16];
    	HexText += HexTable[HexValue % 16];
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Assuming the file is ASCII encoded:
    Code:
    string text = "BlahDiBlahBlahBlah";
    StringBuilder sb = new StringBuilder(text.Length * 2);
    foreach (byte b in ASCIIEncoding.ASCII.GetBytes(text))
    {
        sb.AppendFormat("{0:x2}", b);
    }
    Console.WriteLine(sb.ToString());

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    7
    Sorry, i was perhaps a bit unclear. The program is for a windows application. the user browses the directory and selects the file. SO I want to know how to read that file to a string, but with a variable i.e string string1.... etc so I can then load the contents of the string into text box (once converted) but just knowing the process to load the string to textbox would be great.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    So Magos gave you how to read the file into a string, and how to set the .Text property to equal a string, and Magos and I both gave you ways to create a hexadecimal string from a regular old string. Seems you should just be able to put A + B + C together to get what you want.

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    7
    Ok, I've done it....basically. There isa bit of cleaning up needs to be done but this is it so far -:

    Code:
    using System;
    using System.IO;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public string ConvertToHex(string asciiString)
            {
                string hex = "";
                foreach (char c in asciiString)
                {
                    int tmp = c;
                    hex += "0x";
                    hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
                    
                    hex += " ";
                }
                return hex;
            }
            public Form1()
            {
                InitializeComponent();
            }
    
            private string fileName;
            private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
            {
                MessageBox.Show("Written by James");
            }
    
            private void openToolStripMenuItem_Click(object sender, EventArgs e)
            {
                OpenFileDialog filechooser = new OpenFileDialog();
                filechooser.Filter = "Text files|*.txt";
    
                DialogResult result = filechooser.ShowDialog();
                if (result == DialogResult.Cancel)
                    return;
    
                fileName = filechooser.FileName;
                if (fileName == "" | fileName == null)
                    MessageBox.Show("Invalid Name", "Error");
    
                else
    
                {
                    richTextBox1.Text ="";
                    try
                    {
                       StreamReader mystream = new StreamReader(fileName);
                        string input = "";
                        string inputhex = "";
                        input += mystream.ReadToEnd();
                        inputhex=ConvertToHex(input);
                        richTextBox1.Text += inputhex;
                        richTextBox2.Text += input;
    
                        string output = "";
                            output+= input;
    
    
                    }
                    catch(IOException)
                        {
                            MessageBox.Show ("Can't open File");
    
    
                        }
    
                        
            }
    
            
        }
    
            private void richTextBox2_TextChanged(object sender, EventArgs e)
            {
                
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
    
            private void saveToolStripMenuItem_Click(object sender, EventArgs e)
            {
    
            }
        }
    }
    However, i want to add a function that will write and convert a new string back to ASCII, and then save to a text file using savefiledialog Any ideas? (the above code works, ignore the empty event handlers, They aren't crucial at this tage)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM