I'm assigning a HEX-Value to a byte in a byte array and then send this byte array to a serial port. If the value is bigger than 255 the corresponding HEX-Value is supposed to be split into two.
Example: 750 = 0x2EE so A[0] = 0x2 and A[1] = 0xEE
Is this an efficient way to do the task that I want it to do? From what I read so far the conversion should be okay here or can I skip the 0x and still get the same result after the Convert.ToByte?
private void button3_Click(object sender, EventArgs e)
{
byte[] A = new byte[2];
string f = "0x" + Convert.ToInt32(textBox3.Text).ToString("X");
textBox2.Text = Convert.ToString(f.Length); //to test length
if (f.Length < 5)
{
A[0] = 0x00;
A[1] = Convert.ToByte(f,16);
textBox1.Text = "A";
}
else if (f.Length == 5)
{
A[0] = Convert.ToByte(f.Substring(0,3),16);
A[1] = Convert.ToByte("0x" + f.Substring(3),16);
textBox1.Text = "B" + "Sub0: " + f.Substring(0, 3) + "\r\n Sub1 :" + "0x" + f.Substring(3); //to test
}
else
{
textBox1.Text = "Falsche Eingabe";
}
}