-1

how i can return ascii code of a specific string typed in the textbox and show it in same text box?

the code below returns System.byte[], what is the problem?

string value = textBox1.Text;
textBox1.Text=Convert.ToString (Encoding.ASCII.GetBytes("v"));

this answer doesn't work

Community
  • 1
  • 1

1 Answers1

2

The call to Encoding.ASCII.GetBytes returns a byte array. You can't just convert the whole object to a string, or you get the class type (System.byte[] in this case).

Instead, call ToString on each item (byte) in the collection:

textBox1.Text =
    string.Join(",", Encoding.ASCII.GetBytes("hello").Select(b => b.ToString()));

Output:

"104,101,108,108,111"

Or if you don't want a delimiter:

textBox1.Text = new string(
    Encoding.ASCII.GetBytes("hello").SelectMany(b => b.ToString()).ToArray());

Output:

"104101108108111"
Grant Winney
  • 65,241
  • 13
  • 115
  • 165