![]() |
| | #1 |
| Registered User Join Date: Mar 2008
Posts: 7
| C# problem 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";
Code:
StreamReader streamReader = new StreamReader(OpenFileDialog.dialog);
string text = streamReader.ReadToEnd();
streamReader.Close();
|
| JST212 is offline | |
| | #2 |
| Confused 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);
}
__________________ 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 | |
| | #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 | |
| | #4 |
| Confused 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 | |
| | #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 | |
| | #6 |
| Confused 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 | |
| | #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 | |
| | #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 | |
| | #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 | |
| | #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)
{
}
}
}
|
| JST212 is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |