如何拆分一个字节数组 [英] How to split a byte array
问题描述
我在内存中的字节数组,从文件中读取。我想,而不必只需要创建一个新的字节数组,每个字节一次复制在某一个点(指数)将字节数组拆分,增加了操作的内存足迹。我想是这样的:
字节[] largeBytes = [1,2,3,4,5,6,7,8,9];
字节[] smallPortion;
smallPortion =拆分(largeBytes,3);
smallPortion就等于1,2,3,4结果
largeBytes就等于5,6,7,8,9
块引用>感谢您,
基思编辑:
@迈克尔,尼斯code ..我看到这个例子是如何工作的,只需创建特定的意见原始数组。
有关其他人只是少数FYI问题谁可以读取这一点。
通过生成的视图时,其他类使用你如何看这方面的工作?如何参考原始数组范围留?
我猜,因为你实际上将被传递到意见中引用的其他类将继续访问相同的方式查看您的主要例子。该视图包含一个私人参照原数组的范围保持并不会GC'ed。
我想,这就是答案。
感谢您,基思
顺便说一句,我喜欢这个网站!
解决方案这是我会怎么做:
使用系统;
System.Collections中使用;
使用System.Collections.Generic;类ArrayView< T> :IEnumerable的< T>
{
私人只读T []数组;
私人只读INT偏移,计数; 公共ArrayView(T []数组,诠释抵消,诠释计数)
{
this.array =阵列;
this.offset =偏移;
this.count =计数;
} 公众诠释长度
{
{返回计数; }
} 公共ŧ这个[INT指数]
{
得到
{
如果(指数℃,||索引> = this.count)
抛出新IndexOutOfRangeException();
其他
返回this.array [偏移+指数]
}
组
{
如果(指数℃,||索引> = this.count)
抛出新IndexOutOfRangeException();
其他
this.array [偏移+指数] =值;
}
} 公众的IEnumerator< T>的GetEnumerator()
{
的for(int i =偏移; I<胶印+计数;我++)
产量返回的数组[我]
} 的IEnumerator IEnumerable.GetEnumerator()
{
IEnumerator的< T>枚举= this.GetEnumerator();
而(enumerator.MoveNext())
{
产量回报enumerator.Current;
}
}
}类节目
{
静态无效的主要(字串[] args)
{
字节[]改编= {1,2,3,4,5,6,7,8,9,0};
ArrayView<位> P1 =新ArrayView<位>(ARR,0,5);
ArrayView<位> P2 =新ArrayView<位>(ARR,5,5);
Console.WriteLine(第一阵列:);
的foreach(在P1字节B)
{
Console.Write(二);
}
Console.Write(\\ n);
Console.WriteLine(第二阵:);
的foreach(在P2字节B)
{
Console.Write(二);
}
Console.ReadKey();
}
}I have a byte array in memory, read from a file. I would like to split the byte array at a certain point(index) without having to just create a new byte array and copy each byte at a time, increasing the in memory foot print of the operation. What I would like is something like this:
byte[] largeBytes = [1,2,3,4,5,6,7,8,9]; byte[] smallPortion; smallPortion = split(largeBytes, 3);
smallPortion would equal 1,2,3,4
largeBytes would equal 5,6,7,8,9Thank you, Keith
EDIT: @Michael, Nice code..
I see how this example works, by just creating specific "views" of the original array.
Just a few FYI questions for others who may read this later.
How would you see this working when passing the resultant view to other classes for use? How does the reference to the original array stay in scope?
I guess since you would actually be passing references to the "views", the other classes will continue to access the view in the same manner as the example in your Main. The View contains a private reference to the original array keeping in in scope and would not be GC'ed.
I think this is the answer.
Thank you, Keith
BTW, I love this site!
解决方案This is how I would do that:
using System; using System.Collections; using System.Collections.Generic; class ArrayView<T> : IEnumerable<T> { private readonly T[] array; private readonly int offset, count; public ArrayView(T[] array, int offset, int count) { this.array = array; this.offset = offset; this.count = count; } public int Length { get { return count; } } public T this[int index] { get { if (index < 0 || index >= this.count) throw new IndexOutOfRangeException(); else return this.array[offset + index]; } set { if (index < 0 || index >= this.count) throw new IndexOutOfRangeException(); else this.array[offset + index] = value; } } public IEnumerator<T> GetEnumerator() { for (int i = offset; i < offset + count; i++) yield return array[i]; } IEnumerator IEnumerable.GetEnumerator() { IEnumerator<T> enumerator = this.GetEnumerator(); while (enumerator.MoveNext()) { yield return enumerator.Current; } } } class Program { static void Main(string[] args) { byte[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; ArrayView<byte> p1 = new ArrayView<byte>(arr, 0, 5); ArrayView<byte> p2 = new ArrayView<byte>(arr, 5, 5); Console.WriteLine("First array:"); foreach (byte b in p1) { Console.Write(b); } Console.Write("\n"); Console.WriteLine("Second array:"); foreach (byte b in p2) { Console.Write(b); } Console.ReadKey(); } }
这篇关于如何拆分一个字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!