将字节数组转换为一定长度的数组段

我有一个字节数组,我想返回一定大小的顺序块(以新字节数组的形式)。


我试过:


originalArray = BYTE_ARRAY

var segment = new ArraySegment<byte>(originalArray,0,640); 

byte[] newArray = new byte[640];

for (int i = segment.Offset; i <= segment.Count; i++)

{

newArray[i] = segment.Array[i];

}

显然,这只从原始数组创建了一个前 640 个字节的数组。最终,我想要一个遍历前 640 个字节并返回这些字节数组的循环,然后遍历下一个 640 个字节并返回一个 THOSE 字节数组。这样做的目的是向服务器发送消息,每条消息必须包含 640 个字节。我不能保证原始数组长度可以被 640 整除。


谢谢


ABOUTYOU
浏览 197回答 3
3回答

侃侃无极

如果速度不是问题var bytes = new byte[640 * 6];for (var i = 0; i <= bytes.Length; i+=640){&nbsp; &nbsp;var chunk = bytes.Skip(i).Take(640).ToArray();&nbsp; &nbsp;...}或者你可以使用跨度切片法Buffer.BlockCopy(Array, Int32, Array, Int32, Int32) 方法跨度Span<byte> bytes = arr; // Implicit cast from T[] to Span<T>...slicedBytes = bytes.Slice(i, 640);块复制请注意,这可能是 3 个中最快的var chunk = new byte[640]Buffer.BlockCopy(bytes, i, chunk, 0, 640);

UYOU

如果您真的想从每个 640 字节块中创建新数组,那么您正在寻找.Skip并且.Take这是我一起破解的一个工作示例(以及示例的副本)。using System;using System.Linq;using System.Text;using System.Collections;using System.Collections.Generic;class MainClass {public static void Main (string[] args) {&nbsp; &nbsp; &nbsp; &nbsp; // mock up a byte array from something&nbsp; &nbsp; &nbsp; &nbsp; var seedString = String.Join("", Enumerable.Range(0, 1024).Select(x => x.ToString()));&nbsp; &nbsp; &nbsp; &nbsp; var byteArrayInput = Encoding.ASCII.GetBytes(seedString);&nbsp; &nbsp; &nbsp; &nbsp; var skip = 0;&nbsp; &nbsp; &nbsp; &nbsp; var take = 640;&nbsp; &nbsp; &nbsp; &nbsp; var total = byteArrayInput.Length;&nbsp; &nbsp; &nbsp; &nbsp; var output = new List<byte[]>();&nbsp; &nbsp; &nbsp; &nbsp; while (skip + take < total) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output.Add(byteArrayInput.Skip(skip).Take(take).ToArray());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; skip += take;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; output.ForEach(c => Console.WriteLine($"chunk: {BitConverter.ToString(c)}"));&nbsp; &nbsp; }}实际使用ArraySegment正确可能会更好——除非这是学习 LINQ 扩展的任务。

撒科打诨

你可以像这样编写一个通用的辅助方法:public static IEnumerable<T[]> AsBatches<T>(T[] input, int n){&nbsp; &nbsp; for (int i = 0, r = input.Length; r >= n; r -= n, i += n)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var result = new T[n];&nbsp; &nbsp; &nbsp; &nbsp; Array.Copy(input, i, result, 0, n);&nbsp; &nbsp; &nbsp; &nbsp; yield return result;&nbsp; &nbsp; }}然后你可以foreach循环使用它:byte[] byteArray = new byte[123456];foreach (var batch in AsBatches(byteArray, 640)){&nbsp; &nbsp; Console.WriteLine(batch.Length); // Do something with the batch.}或者,如果您想要批次列表,请执行以下操作:List<byte[]> listOfBatches = AsBatches(byteArray, 640).ToList();如果您想变得花哨,可以将其作为扩展方法,但仅在您将大量使用它时才建议这样做(不要为您只会在一个地方调用的东西创建扩展方法!)。在这里,我更改了名称InChunksOf()以使其更具可读性:public static class ArrayExt{&nbsp; &nbsp; public static IEnumerable<T[]> InChunksOf<T>(this T[] input, int n)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0, r = input.Length; r >= n; r -= n, i += n)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var result = new T[n];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Array.Copy(input, i, result, 0, n);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return result;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}你可以这样使用:byte[] byteArray = new byte[123456];// ... initialise byteArray[], then:var listOfChunks = byteArray.InChunksOf(640).ToList();[编辑] 将循环终止符从 更正r > n为r >= n。
打开App,查看更多内容
随时随地看视频慕课网APP