0

I have a data string data2 = " %04%02%BC%94%BA%15%E3%AA%08%00%7F%00"; I am trying to split every two digits between the % sign and put it into an array.

In addition to that, if there is extra digit, i.e. more than 2 digits, convert to Hex and add it to the array.

My code is working sometimes, but when I add extra digit in the second last position, it gives wrong values.

 string data = " %04F%02%BC%94%BA%15%E3%AA%08%00%7FF%00";

        List<string> Values = new List<string>();

        string[] val = Regex.Split(data2, "%");
        byte[] TempByte = new byte[val.Length - 1];


        for (int i = 0; i < val.Length; i++)
        {
            Values.Add(val[i]);

            if (Values[i].Length > 2)
            {
                //count
                int count = 0;
                int n = 2;                       //start from digit 2(if ther is any)
                foreach (char s in Values[i])
                {
                    count++;
                }
                int index = count - 2;         //index starting at 2

                while (n <= Values[i].Length -1)    
                {
                    string temp = string.Join(string.Empty, Values[i].Substring(n, 1).Select(c =>
                                                        ((int)c).ToString("X")).ToArray());


                    Values.Add(temp);
                    n = n + 1;
                }
                //remove the extra digit
                Values[i] = Values[i].Replace(Values[i].Substring(2, 1), string.Empty);

            }
        }


        Values.RemoveAt(0);                        //since digit 0 is always zero
        string[] TagTemp = Values.ToArray();

//Convert to array

        for (int i = 0; i < val.Length - 1; i++)
        {
            TempByte[i] = Convert.ToByte(TagTemp[i], 16);
        }

When extra digit is added to the first position, i.e 04F, the output is correct:

enter image description here

When it is added second last position, i.e 7FF instead of 7F 46 it gives just 7.

enter image description here

Do you guys see what is wrong and how to fix it?

Liban
  • 641
  • 5
  • 19
  • 32
  • How is the output correct? How do you get `46` from either `0x04` or `0x4F`, depending on how you want to treat the extra hex digits? – Roger Lipscombe Mar 18 '13 at 09:48
  • @RogerLipscombe - `04F` is translated into `04 46`. `F` is converted into hex – Liban Mar 18 '13 at 10:14
  • and how is that "correct"? `0x04F` is `79`. `0x04` is `4`. `0x4F` is still `79`. How do you get `04 46` from `0x04F`. In no number system that I'm familiar with does this happen... – Roger Lipscombe Mar 18 '13 at 10:55
  • @RogerLipscombe, no it is not like that.. the `04` is left as it is, just the extra `F` is converted, which is `46`.. it is some kind of protocol.. – Liban Mar 18 '13 at 11:00
  • Oh, OK. You've got `%04` `"F"` `%02`... – Roger Lipscombe Mar 18 '13 at 11:13

3 Answers3

2

You can't convert a three-digit hexadecimal string to a byte. The maximum value a byte can hold is FF.

Konamiman
  • 49,681
  • 17
  • 108
  • 138
2
     string data = " %04F%02%BC%94%BA%15%E3%AA%08%00%7FF%00";

     // You need to pick an encoding -- are these things ASCII?
     var encoding = Encoding.ASCII;
     var values = new List<byte>();

     // Walk over the data (note that we don't increment here).
     for (int i = 0; i < data.Length;)
     {
        // Is this the start of an escaped byte?
        if (data[i] == '%')
        {
           // Grab the two characters after the '%'.
           var escaped = data.Substring(i + 1, 2);
           //Console.WriteLine(escaped);

           // Convert them to a byte.
           byte value = Convert.ToByte(escaped, 16);
           values.Add(value);

           // Increment over the three characters making up the escaped byte.
           i += 3;
        }
        else
        {
           // It's a non-escaped character.
           var plain = data[i];
           //Console.WriteLine(plain);

           // Convert it to a single byte.
           byte[] bytes = encoding.GetBytes(new[] { plain });
           Debug.Assert(bytes.Length == 1);
           byte value = bytes[0];

           values.Add(value);

           // Increment over that character.
           i += 1;
        }
     }

     // Print it out, in hex, separated by commas.
     Console.WriteLine(string.Join(", ",
                       values.Select(v => string.Format("{0:X2}", v))));

     // Alternatively...
     Console.WriteLine(BitConverter.ToString(values.ToArray()));
Roger Lipscombe
  • 89,048
  • 55
  • 235
  • 380
1

Values[i].Replace(Values[i].Substring(2, 1), string.Empty); is replacing both F's rather than just the one

String.Replace()

see this post for an example of positional replace.

Community
  • 1
  • 1
qujck
  • 14,388
  • 4
  • 45
  • 74
  • the index `substring(2,1)` refers to the third digit i think.. also it works well when extra character is added to other positions, like the one i showed above.. – Liban Mar 18 '13 at 10:23
  • see the MSDN link I supplied: `String.Replace()` returns a new string in which all occurrences of a specified Unicode character in this instance are replaced with another specified Unicode character. `Substring(2,1)` finds the F, `Replace()` replaces them all. – qujck Mar 18 '13 at 10:28