Vesa95
Vesa95

Reputation: 627

Custom string placeholders in C#

Right now, I'm working to an application which receive a series of bytes and send them via serial, but it's a little bit cumbersome because for example I have this scenario: I want to send via serial two bytes with a special meaning, then two float and then a string:

00 01 02 3F 00 00 00 3F C0 00 00 4E 41 4D 45

where: 00 01 02 have a special meaning then 3F 00 00 00 is 0.5 and 3F C0 00 00 is 1.5 and last 4E 41 4D 45 is the string NAME.

As you can see I need to do the conversion manually an then form the data which I want to send, but what I want is to creating a string formatter of some type of placeholder for automatically converting this example of DATA (as a string): "00 01 02 [0.5] [1.5] [NAME]"

So instead of introducing this not so nice data in a text-box "00 01 02 3F 00 00 00 3F C0 00 00 4E 41 4D 45" I want to introduce this string of data "00 01 02 [0.5] [1.5] [NAME]" and then a parser should know to convert to float or to string when the square bracket is met.

I other project I used a function which replace the "//" from a string with a specific data, but I need to adapt it, and also not sure if it's the right way to do this:

private string dataParser(string input)
{
    string s = input;
    string[] tokens = s.Split(new[] { "//" }, StringSplitOptions.None);
    StringBuilder sb = new StringBuilder();

    int counter = 0;
    for (int i = 0; i < tokens.Length; i++)
    {
        sb.Append(tokens[i]);
        if (i < tokens.Length - 1)
            sb.Append(hexfloatArray[counter++]);
    }

    return sb.ToString().Replace(" ", "");
}

Upvotes: 0

Views: 254

Answers (1)

Anon Coward
Anon Coward

Reputation: 10823

You can use a regular expression fairly easily to convert the tokens that are in brackets to different values.

var value = "00 01 02 [0.5] [1.5] [NAME]";

// Replace something like [x.y] with it's byte string
value = Regex.Replace(value, "\\[(\\d+\\.\\d+)]", (m) =>
{
    // This is the number from the section
    var number = float.Parse(m.Groups[1].Value);
    // Encode it as Ff + bytes from number for this example
    string ret = "FF";
    foreach (var x in BitConverter.GetBytes(number))
    {
        ret += " " + x.ToString("x2");
    }
    return ret;
});

// Replace anything else in brackets as if it's a string
value = Regex.Replace(value, "\\[(.*?)]", (m) =>
{
    // This is the string from the section
    var str = m.Groups[1].Value;
    // Encode it as FE + UTF8 bytes for this example
    string ret = "FE";
    foreach (var x in Encoding.UTF8.GetBytes(str))
    {
        ret += " " + x.ToString("x2");
    }
    return ret;
});

Console.WriteLine(value);
// Shows: 00 01 02 FF 00 00 00 3f FF 00 00 c0 3f FE 4e 41 4d 45

The idea here is to find the different special cases, and convert them to the bytes you'd expect in the string in the lambda expressions. I've shown a simple case of using BitConverter and UTF-8 encoding along with a simple single byte, but you'll need to change to whatever encoding you need.

Upvotes: 1

Related Questions