How to set hexadecimal input ranges for different segments in a data message?

Records of a 24-byte byte: xx xx xx xx xx xx xx xx xx xx xx xx

I have a record with a 24-character byte in a text box with different write requirements in the byte segment, do I have a template code for dynamically encoding the range of hexadecimal values ​​that is required for each segment, for example. 14?

I tried the code below, but that’s not what I wanted, as it converts the entire hexadecimal notation to the int equivalent, without giving me the freedom to encode 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 hexadecimal values ​​entered in a text field, the function for checking the entered hexadecimal value is between 0 and 100 in uint before converting it to the decimal equivalent in the output text field, otherwise it will display “Error” in the output text field .

Subsequently, the next 4 bytes, I only allow 4 bytes of hexadecimal entries in the range -1000 to 1000 in int else. Displays an error message.

EDIT: Sorry for the confusion as I expressed my suggestions before, since 5 bytes is just a byte representation of the ASCII character for each byte with a range from 0 to 255. How to encode these different cases? Thank!

uint value1 = reader.ReadByte(); // 1 byte.
    if (value1 ==700)
    {
                   OutputTxt.Text = value1.ToString();
                   OutputTxt.Text +="\n";
    }
    else
    {
                OutputTxt.Text = "Error"
                OutputTxt.Text +="\n";
    }

   uint value2 = reader.ReadByte(); // 1 byte.
    if (value2 <=1000)
    {
                   OutputTxt.Text += value2.ToString();
                   OutputTxt.Text +="\n";
    }
    else
    {
                OutputTxt.Text += "Error"
                OutputTxt.Text += "\n";
    }

, , , readByte()?

+4
1

, - - :

  • .
  • MemoryStream BinaryReader, .
  • , .

- , , 5 .

5 , BitConverter, long. :

public static long LongFromBytes(byte[] bytes)
{
    // This uses little-endian format. If you're expecting bigendian, you
    // would need to reverse the order of the bytes before doing this.

    return BitConverter.ToInt64(bytes.Concat(new byte[8-bytes.Length]).ToArray(), 0);
}

( ) :

public static byte[] StringToByteArray(string hex)
{
    hex = hex.Trim().Replace(" ", "");

    return Enumerable.Range(0, hex.Length)
        .Where(x => x % 2 == 0)
        .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
        .ToArray();
}

:

string hexString = "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18";

using (var stream = new MemoryStream(StringToByteArray(hexString)))
using (var reader = new BinaryReader(stream))
{
    int value1 = reader.ReadByte(); // 1 byte.
    Console.WriteLine(value1);

    int value2 = reader.ReadByte(); // 1 byte.
    Console.WriteLine(value2);

    int value3 = reader.ReadInt32(); // 4 bytes.
    Console.WriteLine(value3);

    long value4 = LongFromBytes(reader.ReadBytes(5)); // 5 bytes into a long.
    Console.WriteLine(value4);
}

, .

+3

All Articles