C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-16-2008, 07:26 AM   #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.
JST212 is offline   Reply With Quote
Old 04-16-2008, 07:34 AM   #2
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,125
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.
Magos is offline   Reply With Quote
Old 04-16-2008, 08:05 AM   #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
JST212 is offline   Reply With Quote
Old 04-16-2008, 08:08 AM   #4
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,125
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.
Magos is offline   Reply With Quote
Old 04-16-2008, 08:14 AM   #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.
JST212 is offline   Reply With Quote
Old 04-16-2008, 08:40 AM   #6
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,125
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.
Magos is offline   Reply With Quote
Old 04-16-2008, 08:52 AM   #7
and the Hat of Ass
 
Join Date: Dec 2007
Posts: 810
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());
rags_to_riches is offline   Reply With Quote
Old 04-17-2008, 05:27 PM   #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.
JST212 is offline   Reply With Quote
Old 04-17-2008, 06:07 PM   #9
and the Hat of Ass
 
Join Date: Dec 2007
Posts: 810
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.
rags_to_riches is offline   Reply With Quote
Old 04-22-2008, 05:59 AM   #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)
JST212 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 04:32 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22