Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用c#中的特定数组值拆分字节数组?_C#_Asp.net_Arrays - Fatal编程技术网

如何用c#中的特定数组值拆分字节数组?

如何用c#中的特定数组值拆分字节数组?,c#,asp.net,arrays,C#,Asp.net,Arrays,如何用c#中的特定数组值拆分字节数组 我只想通过“59”分割数组,这样就可以得到3字节数组。 我试了很多次,都找不到解决办法。提前感谢您可以使用数组。IndexOf(大字节,(字节)59,索引)其中索引将是循环中最后一个找到的索引(开始时为0),直到函数返回-1(在数组中不再找到59)。 在由(byte)59创建的边界上,复制一些子数组,如回答中所述:以下是如何实现这一点的算法 //Here I'm not including 59 in the sub arrays var

如何用c#中的特定数组值拆分字节数组

我只想通过“59”分割数组,这样就可以得到3字节数组。
我试了很多次,都找不到解决办法。提前感谢您可以使用
数组。IndexOf(大字节,(字节)59,索引)
其中索引将是循环中最后一个找到的索引(开始时为0),直到函数返回
-1
(在数组中不再找到
59
)。
在由
(byte)59
创建的边界上,复制一些子数组,如回答中所述:

以下是如何实现这一点的算法

 //Here I'm not including 59 in the sub arrays  
       var largeBytes = new byte[] {70,68,49,59,117,49,59,112};
        var lists = new List<List<byte>>();
        const int marker = 59;
        var tempLst = new List<byte>();
        foreach (var largeByte in largeBytes)
        {


            if (largeByte==marker)
            {
                lists.Add(tempLst);               
                tempLst=new List<byte>();
            }
            else
            {
                tempLst.Add(largeByte);    
            }

        }
        lists.Add(tempLst);
//这里我没有在子数组中包含59
var largeBytes=新字节[]{70,68,49,59117,49,59112};
var List=新列表();
常量int标记=59;
var templast=新列表();
foreach(var largeByte in largeBytes)
{
if(大字节==标记)
{
列表。添加(模板);
tempLst=新列表();
}
其他的
{
模板添加(大字节);
}
}
列表。添加(模板);

您可以使用IEnumerable的
GroupBy
执行拆分:

byte[] largeBytes = new byte[] {70,68,49,59,117,49,59,112};
byte split = 59;
long index = 0;
var results = largeBytes.GroupBy(b => index += Convert.ToInt64(b==split)); 
foreach (var result in results) {
    Console.WriteLine($"Group Key: {result.Key}");
    foreach (var value in result) {
        Console.WriteLine($" - Value: {value}");
    }
}
为了好玩,这里有一种使用C#7元组的方法:

byte[] largeBytes = new byte[] {70,68,49,59,117,49,59,112};
byte split = 59;
long index = 0;
var results = largeBytes.Select(x => ((index += Convert.ToInt64(x == 59)),x));
foreach (var tuple in results) { 
    Console.WriteLine($"{tuple.Item1}: {tuple.Item2}");
}

演示:

最简单的解决方案是使用以下扩展方法:

这将返回一个IEnumerable的
IEnumerable
。您可以使用
ToArray()
将其转换为
IEnumerable

或者,您可以大致执行扩展方法的操作—创建一个迭代器,检查每个输入元素,直到找到分隔符并将每个字符放入数组:

public static IEnumerable<List<T>> Split<T>(this IEnumerable<T> source, T separator)
{
    List<T> result=new List<T>(3);
    foreach(T item in source)
    {
        if (item == separator)
        {
            yield return result;
            result=new List<T>(3);
        }
        else 
        {
            result.Add(item);
        }
    }
    yield return result;
}

MoreLINQ的版本更加通用,因为它允许您指定最大拆分数,或者将输入转换为另一种形式

如果要包含分隔符,请将
result.Add
放在第一个
If
之前。更好的选择是添加
include
参数:

public static IEnumerable<List<T>> Split<T>(this IEnumerable<T> source, T separator,bool include=false)
{
    List<T> result=new List<T>(3);
    foreach(T item in source)
    {
        if (item == separator)
        {
            if (include) result.Add(item);
            yield return result;
            result=new List<T>(3);
        }
        else 
        {
            result.Add(item);
        }
    }
    yield return result;
}
公共静态IEnumerable拆分(此IEnumerable源代码,T分隔符,bool include=false)
{
列表结果=新列表(3);
foreach(源中的T项)
{
如果(项==分隔符)
{
如果(包括)结果,则添加(项目);
收益结果;
结果=新列表(3);
}
其他的
{
结果.添加(项目);
}
}
收益结果;
}

类似问题:。同样的方法也可以应用于使用标记的十进制值。是否包含59字节
var triplets=largeBytes.Split(separator).Select(triplet=>triplet.ToArray());
public static IEnumerable<List<T>> Split<T>(this IEnumerable<T> source, T separator)
{
    List<T> result=new List<T>(3);
    foreach(T item in source)
    {
        if (item == separator)
        {
            yield return result;
            result=new List<T>(3);
        }
        else 
        {
            result.Add(item);
        }
    }
    yield return result;
}
byte separator=59;
var triplets=largeBytes.Split(separator);
var triplets=MyExtensionsClass.Split(largeBytes,separator);
public static IEnumerable<List<T>> Split<T>(this IEnumerable<T> source, T separator,bool include=false)
{
    List<T> result=new List<T>(3);
    foreach(T item in source)
    {
        if (item == separator)
        {
            if (include) result.Add(item);
            yield return result;
            result=new List<T>(3);
        }
        else 
        {
            result.Add(item);
        }
    }
    yield return result;
}