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

像这样的东西在一般情况下应该如何工作(显然有更好的错误检查!):

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        byte[] exampleArray = new byte[] { 1, 2, 3, 4, 5, 2, 3, 6, 7, 8 };

        var test = exampleArray.PartitionBySubset(new byte[] { 2, 3 }).ToList();
    }
}

public static class Extensions
{
    public static IEnumerable<IEnumerable<T>> PartitionBySubset<T>(this IEnumerable<T> sequence, IEnumerable<T> subset) where T : IEquatable<T>
    {
        // Get our subset into a array so we can refer to items by index and so we're not iterating it multiple times.
        var SubsetArray = subset.ToArray();
        // The position of the subset array we will check
        var SubsetArrayPos = 0;
        // A list to hold current items in the subsequence, ready to be included in the resulting sequence
        var CurrentList = new List<T>();

        foreach (var item in sequence)
        {
            // add all items (ones part of the subset will be removed)
            CurrentList.Add(item);
            if (item.Equals(SubsetArray[SubsetArrayPos]))
            {
                // This item is part of the subset array, so move to the next subset position
                SubsetArrayPos++;
                // Check whether we've checked all subset positions
                if (SubsetArrayPos == SubsetArray.Length)
                {
                    // If so, then remove the subset items from the current list
                    CurrentList.RemoveRange(CurrentList.Count - SubsetArray.Length, SubsetArray.Length);
                    // Reset the position
                    SubsetArrayPos = 0;

                    // Only return the list if it's not empty (the main list could start with a subset)
                    if (CurrentList.Count != 0)
                    {
                        // Return the list we have now since it's been ended.
                        yield return CurrentList;
                        // Create a new list ready for more items
                        CurrentList = new List<T>();
                    }
                }
            }
            else
            {
                // This item isn't part of the subset, so next time check the start.
                SubsetArrayPos = 0;
            }
        }

        // If we get to the end and have some items to return, then return them.
        if (CurrentList.Count != 0)
            yield return CurrentList;
    }
}
于 2013-04-29T15:06:56.580 回答
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 回答