|
Answer» Im trying to convert a STRING/plain text from a textbox to a byte array.
For example: byte[] BYTES = new byte[] {0x00, 0x00, 0x00};
I want to do byte[] Bytes = new byte[] {Textbox.Text}; And what I will put in the textbox will be 0x00 0x00 0x00You should probably specify the language you're using, but I'm going to assume that's C# and that you're using Visual Studio. I also don't fully UNDERSTAND what you're trying to do, but I'm pretty sure you want to get a byte array that CONTAINS the ASCII values of characters in a text box.
Code: [Select]using Microsoft.VisualBasic; char[] Characters = Textbox.Text.ToCharArray(); byte[] Bytes = new byte[Characters.Length - 1]; for (int i = 0; i < Characters.Length; i++) { byte[i] = String.Asc(Characters[i]); }Code: [Select]byte[] results = (from p in str.ToCharArray() select (byte)p).ToArray();
It looks like DaftHacker WANTS to go a bit further and actually have the textbox contain the Hex codes of each byte. Not a problem:
Code: [Select] String[] results = (from p in TextBox1.Text.ToCharArray() select String.Format("0x{0:X2}",(byte)p)).ToArray(); TextBox1.Text = String.Join(" ", results);
|