0

I´ve got an byte array where variouse bits have a meanings. Now I´m wondering if I should use the interpreter pattern to define the array´s meaning and the pattern builds some dto´s out of the definition.

new Interpreter(arr)
  .Interpret("B0.01", ref MyObject.MyValue)
  .Interpret("B0.02", ref MyObject.SecondValue);

I´m unsure if its the best solution. May someone tell if there is a similiar project on the net or maybe a better solution? I´m using C# .net-framework 4.6.1

[EDIT]

For example I´ve got the following Byte-Array

var arr = new[] { 0b00010010, 0b11100101, aso...}

Now a documentation may say:

Byte 0, Bit 0 -> Person has blue hair
Byte 0, Bit 1 -> Person owns a house
Byte 1, Bit 7 -> The house is gray

Now I want parse from this array maybe two objects as following

public class Person 
{
    public bool HasBlueHair { get; }
    public bool OwnsHouse { get; }
}

public class House 
{
    public bool IsGray { get; }
}

I thought maybe the interpreter pattern is a good solution for this purpose but I´m not very familiar with it, so I need some help.

5
  • 1
    I'm sorry, I don't follow you: what is "bits have a meaning?", what is "interpreter pattern" in context of your problem? Could you, please, provide some examples of input data and the desired outcome? For instance, having byte[] array = new byte[] {0x0A, 0x0B, 0x00, 0x12} what is the desired result? Commented Dec 6, 2023 at 15:02
  • 1
    This is not a suitable problem for the Interpreter Pattern. Commented Dec 6, 2023 at 15:23
  • 1
    Can a sequence of bits be tokenized into an expression in a self-contained manner, or can the sequence be tokenized only with the help of the expected content as defined in the docs? If the latter, this looks more like serialization, with the documentation defining a serialization contract for the data. Commented Dec 6, 2023 at 15:25
  • Generally, you do this with logical bit operations (possibly with a [Flags] enum) Commented Dec 6, 2023 at 16:41
  • 1
    This (my answer to a loosely related question) may provide some inspiration: stackoverflow.com/questions/55499116/… Commented Dec 6, 2023 at 16:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.