tags:

views:

59

answers:

2

Can anyone help, I'm using visual c# express edition 2008 obviously dealing with c#. I have a form with two text boxes, in one I want to show a .txt file in text format and in the other text box I want to show the same text but in hex. I have no idea how to do this as programming isn't a strong point of mine. Also its got to be in whole columns and show the value of each character in the form '0xnn'. Incase anyone recognises this I did ask a similar question earlier today, but have since progressed with some of it.

A: 

The easiest way would probably be to convert the text to a byte[],

byte[] bytes = Encoding.ASCII.GetBytes(textFromFile);

and use BitConverter.ToString() to build a hex list of the byte[].

string hexListing = BitConverter.ToString(bytes)
James Curran
How do you convert the text to a numeric representation, and then to a byte[]? That's non-trivial for a neophyte.
Robert Harvey
@Robert: FGITW -- Post the overview, then lookup (and repost) the details.
James Curran
+1  A: 
        StringBuilder sb = new StringBuilder();
        foreach (char character in File.ReadAllText("input.txt").ToCharArray())
        { //convert the string to array of bytes
            sb.Append("0x"+      ///"0x" prefix
               ((int)character). //convert char to int
               ToString("X2"));  //generate string with two hex digit.
            sb.Append("\n");         //new line after each converted char
        }

        TextBox1.Text = sb.ToString(); //set text box text
Louis Rhys