-2

I have the following hexadecimal byte array:

Byte[] Bytes = { 0x02, 0x31, 0x20, 0x20, 0x20, 0x20, 0x32, 
                 0x36, 0x38, 0x30, 0x34, 0x03, 0x0D };

And I converted the it to a string using BitConverter:

String value = BitConverter.ToString(Bytes);

At this point, value looks like:

"02-31-20-20-20-20-32-36-38-30-34-03-0D"

Now I need to eliminate the values from 02 to the last 20 and from 03 to 0D. In other words, I need to extract 32-36-38-30-34 by eliminating the rest.

The key point is that 0x02 in hexadecimal represents "Start of Text", 0x20 represents a space, and 0x03 represents "End of Text". So I need to eliminate all the values from "Start of Text" to the last space, and from "End of Text" to obtain values between them.

Rufus L
  • 36,127
  • 5
  • 30
  • 43
Sumi
  • 157
  • 1
  • 3
  • 15
  • What have you tried? What _specifically_ is not working? – maccettura Aug 30 '18 at 16:55
  • What if you have a "space" inside the actual text? Are you sure this is not a possibility? – 41686d6564 stands w. Palestine Aug 30 '18 at 17:03
  • Space should be eliminated – Sumi Aug 30 '18 at 17:04
  • What about the `0x31`? Why are you eliminating that? Does the array *always* start with `0x02`? Does it *always* contain a `0x03`? What if there was an `0x02` character after `0x32`? Do you remove it? Do you remove the `0x32` also since it's before the last space? Knowing these things will make the algorithm much easier to create. – Rufus L Aug 30 '18 at 17:06
  • @Sumi You need to be specific. If you want to remove everything until the last space, then are you sure you won't have any "spaces" inside the actual text that you want to extract? If you want to remove all spaces, then why is `0x31` being removed? – 41686d6564 stands w. Palestine Aug 30 '18 at 17:07
  • Moreover, if you already have a byte array in the beginning, you probably don't need regex. You can remove what you want from the array (e.g., in a for loop or using `SkipWhile` and `TakeWhile`) before converting it to a string. Is there a reason you want to use regex? – 41686d6564 stands w. Palestine Aug 30 '18 at 17:10
  • @Rufus L Array always starts with 0x02 and it always contains 0x03. Actual value to be extracted always starts after the last 0x20 and ends before 0x03. – Sumi Aug 30 '18 at 17:10
  • What if there is no `0x20`? – Rufus L Aug 30 '18 at 17:11
  • @Rufus L .Values will be like the following sample. 02 31 20 20 20 20 32 38 39 37 31 03 0D 0A 02 31 20 20 20 20 32 38 39 35 30 03 0D 0A 02 31 20 20 20 20 32 39 30 30 30 03 0D 0A 02 31 20 20 20 20 32 38 38 38 34 03 0D 0A – Sumi Aug 30 '18 at 17:15
  • After every 0x0A(Line Feed) , Again starts with 0x02 – Sumi Aug 30 '18 at 17:17
  • But to build an algorithm, you use actual **rules**. You have to consider samples that may not be what you expect. That's why people are asking for clarification. What if there is no `0x20`? Why do you care about `0x02` if you start after the last `0x20`? What if there is no `0x03`? I assume in these cases you would just return an empty string, but you need to explicitly call out all the rules here. Even now in the comments you are suddenly mentioning another input (`0x0A`) which was not discussed in the question. – Rufus L Aug 30 '18 at 17:18

1 Answers1

0

Here's a method that takes in a string, splits it on the Line Feed string ("0A") to get the lines of data, then takes each line and adds everything after the first space ("20") up to the end of line string ("03"), and returns a list of these strings:

public static List<string> GetData(string allData)
{
    var newLine = "0A";
    var space = "20-";
    var endText = "-03";

    var data = new List<string>();
    if (string.IsNullOrWhiteSpace(allData)) return data;

    var lines = allData.Split(new[] { newLine }, StringSplitOptions.RemoveEmptyEntries);

    foreach (var line in lines)
    {
        var lastSpace = line.LastIndexOf(space);
        if (lastSpace == -1) continue;

        var endOfText = line.IndexOf(endText, lastSpace);
        if (endOfText == -1) continue;

        var start = lastSpace + space.Length;
        var length = endOfText - start;

        data.Add(line.Substring(start, length).Trim('-'));
    }

    return data;
}

Here it is in use using your sample data from the question and from the comment. Note that this assumes a string that's been converted already using BitConverter:

static void Main()
{
    var questionSample = "02-31-20-20-20-20-32-36-38-30-34-03-0D";

    var commentSample = "02-31-20-20-20-20-32-38-39-37-31-03-0D-0A-02-31-20-20-20-" +
                        "20-32-38-39-35-30-03-0D-0A-02-31-20-20-20-20-32-39-30-" +
                        "30-30-03-0D-0A-02-31-20-20-20-20-32-38-38-38-34-03-0D-0A";

    Console.WriteLine(GetData(questionSample).FirstOrDefault());

    Console.WriteLine(new string('-', Console.WindowWidth - 1));

    foreach (var dataLine in GetData(commentSample))
    {
        Console.WriteLine(dataLine);
    }

    GetKeyFromUser("\nPress any key to exit...");
}

Output

enter image description here

Rufus L
  • 36,127
  • 5
  • 30
  • 43