A 24 hex byte message entries: xx xx xx xx xx xx xx xx xx xx xx xx

I have a 24 hex byte message entry into a textbox with different byte segment entry requirements, how to code dynamically the range of hex value I require for each segment e.g. 1byte, 4, 5byte or any byte requirement?

I tried the code below but is not what I wanted as it converts whole hex entry into int equivalent without giving me the freedom to code the range I wanted.

outputTxt.Text = String.Join(" ", 
    hexTextBox.Text.Trim().Split(' ').Select(item => Convert.ToInt32(item, 16)));

For example, the first group of 2 hex value entered into the textbox, a function to check hex value entered is between 0 to 100 in uint before converting it into its decimal equivalent onto the output textbox, else it will display 'Error' display on the output textbox.

Subsequently the next 4 bytes case, I only allow 4 bytes of hex entries in the range between -1000 to 1000 in int then it can convert to decimal equivalent, else display 'error' message. As for 5 bytes: where each 1 byte is representative to a ASCII characters with range of 0 to 255. How to code these different cases? Thanks!

Recommended Answers

All 5 Replies

So if I understand you well, you have one byte, 4 bytes and the rest are ASCII characters right?

If I understand you correctly, your input will be 2 bytes uint, 4 bytes int, 5 bytes ASCII. For this you will need to create your own parser. The Parse method included in the byte, uint and int structs, includes the option to parse Hex strings. Simply pass in the substring you want parsed:

uint temp = uint.Parse(input.Substring(0, 5).Remove(2,1), System.Globalization.NumberStyles.HexNumber)

From there the Bitconverter class can get the bytes from the integer:

BitConverter.GetBytes(temp)

And can then convert these bytes to single or double precision floating point value:

double 2ByteData = BitConverter.ToDouble(BitConverter.GetBytes(temp), 0)

As for the ASCII simply parse each one as a Byte, which is basically an unsinged 8 bit integer the same as a char.

On a side note you might find it useful to create your own struct to hold each piece of data. This way you can put the parse routine in a separate method and keep your code cleaner.

Sorry for the confusion for how I phase my sentences previously, for the 5 bytes is just a byte representation of a ASCII character for each byte with range of 0 to 255. How to code these different cases for the above scenarios? Therefore, I only require 1 and 4 byte codes that allow me to dynamically its uint and int range representation before decimal conversion.

The examples above should tell you what you need to know. If not, to give a clearer idea of what you want, show an example of the data you expect to receive and what values you expect to parse from it.

Thanks for the advice it worked! Then how to dynamically set the length of a message packet based on the second byte entry. 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?

Byte entries: xx xx xx xx xx xx xx xx xx xx xx xx ..... where x is 0-9, a-f.

The code below was used as a sort of my goal keeper to allow only hex entry into the textbox but how can I enhance the below code with the additional function mention above? 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));
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.