5

次のようなバイト配列があります。

byte[] exampleArray = new byte[] 
                      { 0x01, 0x13, 0x10, 0xe2, 0xb9, 0x13, 0x10, 0x75, 0x3a, 0x13 };

私の最終目標は、シーケンスを見るたびに、この配列をサブ配列に分割すること{ 0x13, 0x10 }です。したがって、サンプル配列での私の望ましい結果は次のようになります。

{ 0x01 }
{ 0xe2, 0xb9 }
{ 0x75, 0x3a, 0x13 }

{ 0x75, 0x3a, 0x13 }理想的には、最後の配列 が検索シーケンスで終わっていないことも知っておく必要があるため、特殊なケースとしてそれを扱うことができます。

最善のアプローチについて何か考えはありますか?

4

4 に答える 4

1
List<byte[]> Split(byte[] bArray)
        {
            List<byte[]> result = new List<byte[]>();
            List<byte> tmp = new List<byte>();
            int i, n = bArray.Length;
            for (i = 0; i < n; i++)
            {
                if (bArray[i] == 0x13 && (i + 1 < n && bArray[i + 1] == 0x10))
                {
                    result.Add(tmp.ToArray());
                    tmp.Clear();
                    i++;
                }
                else
                    tmp.Add(bArray[i]);
            }
            if (tmp.Count > 0)
                result.Add(tmp.ToArray());
            return result;
        }

最後の配列はシーケンスで終了することはできません。分割された部分には区切り文字が含まれていません。バイト 0x13 のみが発生する可能性があるため、これが重要な場合は、最後のサブ配列の最後のバイトを確認できます。

于 2013-04-29T15:21:14.583 に答える
0
string example = Encoding.ASCII.GetString(exampleArray);
string delimiter = Encoding.ASCII.GetString(new byte[] { 0x13, 0x10 });
string[] result = example.Split(new string[] { delimiter});
string ending = Encoding.ASCII.GetString(new byte[] { 0x75, 0x3a, 0x13 });
bool ends = example.EndsWith(ending);
于 2013-04-29T15:09:24.560 に答える
0

現在および前の配列要素のチェックに進みます。このような:

int[] data = ...;

// start from second byte, to be able to touch the previous one
int i = 1;
while (i < data.Length)
{
    if (data[i-1] == 0x13 && data[i] == 0x10)
    {
        // end of subarray reached
        Console.WriteLine();
        i+=2;
    }
    else
    {
        // output the previous character, won't be used in the next iteration
        Console.Write(data[i-1].ToString("X2"));
        i++;
    }
}

// process the last (or the only) byte of the array, if left
if (i == data.Length)
{
    // apparently there wasn't a delimiter in the array's last two bytes
    Console.Write(data[i-1].ToString("X2"));
    Console.WriteLine(" - no 0x13 010");
}

注: デモンストレーションのためのコンソール出力。実際のデータ処理に置き換えます。

于 2013-04-29T15:21:21.030 に答える