Assuming my second byte in hex is a representation of the message length, how can I dynamically adjust the length of the message the user can key into the textbox based on the hex value of the second byte?
Message Byte entries into textbox: xx xx xx xx xx xx xx xx xx xx xx xx ..... where x is 0-9, a-f in hex.
The code below was used as a sort of my goal keeper to allow only hex entry into the textbox but I want to further enhance the code below with the additional function mention above? Sorry if I couldn't explain it clearly enough due to my inadequate English command. Thanks
void textBox1_TextChanged(object sender, EventArgs e)
{
int caret = textBox1.SelectionStart;
bool atEnd = caret == textBox1.TextLength;
textBox1.Text = sanitiseText(textBox1.Text);
textBox1.SelectionLength = 0;
textBox1.SelectionStart = atEnd ? textBox1.TextLength : caret;
}
void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!isHexDigit(e.KeyChar) && e.KeyChar != '\b')
e.Handled = true;
}
string sanitiseText(string text)
{
char[] result = new char[text.Length*2];
int n = 0;
foreach (char c in text)
{
if ((n%3) == 2)
result[n++] = ' ';
if (isHexDigit(c))
result[n++] = c;
}
return new string(result, 0, n);
}
bool isHexDigit(char c)
{
return "0123456789abcdef".Contains(char.ToLower(c));
}