-
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.
-
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.
-
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
-
What do you mean "to hex"?
To enter text into a richtextbox, simply:
Code:
MyRichTextBox.Text = "Hi there!";
-
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.
-
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];
}
-
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());
-
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.
-
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.
-
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)