Computer Hope
Software => Computer programming => Topic started by: DaftHacker on July 25, 2013, 11:31:39 AM
-
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 0x00
-
You 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.
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]);
}
-
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:
String[] results = (from p in TextBox1.Text.ToCharArray() select String.Format("0x{0:X2}",(byte)p)).ToArray();
TextBox1.Text = String.Join(" ", results);