How to set encoding result to TextBox

How do I set the result of my encoding to a TextBox?

string myString;
myString = "Hello World";
byte[] data = Encoding.ASCII.GetBytes(myString);
textBox1.Text = data.ToString();

This displays "System.Byte[]" in the TextBox, but I want to instead show the hex result in the TextBox.

Jon Skeet
people
quotationmark

You can't set the encoding of a text box, but it sounds like you're just trying to display some binary data in a text box... did you want hex, for example? If so, BitConverter.ToString(byte\[\]) is your friend:

textBox1.Text = BitConverter.ToString(data);

... will give you something like 48-65-6C-6C-6F-20-57-6F-72-6C-64. You can using string.Replace to remove the hyphens if you want, e.g.

textBox1.Text = BitConverter.ToString(data).Replace("-", " ");

There are alternative representations of binary data as text, of course. For example, you could use base64:

textBox1.Text = Convert.ToBase64String(data);

But I suspect hex is what you're after.

people

See more on this question at Stackoverflow