简体   繁体   English

从现有字节数组C#的一部分初始化字节数组

[英]Initialize byte array from a portion of existing byte array c#

Is there are an easy way to initialize byte array from portion of existing byte array. 有没有一种简单的方法可以从现有字节数组的一部分初始化字节数组。 In C language it is possible to use pointer to say like 在C语言中,可以使用指针说

char s[10] = new char[10];
char* b = s + 5;

Is it possible to do that in c#, like: 是否可以在c#中做到这一点,例如:

byte[] b = new byte[buffersize];
byte* header  = b + 5;
byte* data = b + 25;

The ArraySegment<T> structure provides a view of an array without creating a copy. ArraySegment <T>结构提供了阵列的视图,而没有创建副本。 It cannot be used in places where a byte array is expected, though. 但是,不能在需要字节数组的地方使用它。

ArraySegment<byte> myArraySegment = new ArraySegment<byte>(myArray, 2, 5);

If you need a byte array, you need to copy the desired values to a new array instance, preferably using Buffer.BlockCopy : 如果需要字节数组,则需要将所需的值复制到新的数组实例,最好使用Buffer.BlockCopy

byte[] newArray = new byte[5];
Buffer.BlockCopy(myArray, 2, newArray, 0, 5);

This is possible, but it requires unsafe code: 这是可能的,但是需要不安全的代码:

unsafe 
{
    byte[] b = new byte[buffersize];
    fixed(byte* bStart = b)
    {
       byte* header = bStart + 5;
       byte* data = bStart + 25;
    }
}

That being said, this is generally avoided unless absolutely necessary in C#. 话虽如此,除非在C#中绝对必要,否则通常避免这样做。 Using unsafe code is not the norm because it is, as suggested, not safe. 使用不安全的代码不是规范,因为按照建议,它是不安全的。 This type of code can lead to many bugs, so it's best to rethink the design. 这种类型的代码可能会导致许多错误,因此最好重新考虑设计。

Sure there is. 当然可以。

int[] myArray = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] subArray = myArray.Skip(5).ToArray();

The peformance characteristics are very different than your C example, however they are really not terrible. 性能特征与您的C示例完全不同,但是它们确实并不可怕。 If you're looking for performance, than use unsafe as above, use the same array and simply begin iterating from the point in the list that you want, or use Array.Copy as shown elsewhere. 如果您要寻找性能,则可以像上面那样使用不安全的方法,使用相同的数组,然后简单地从列表中的所需位置开始迭代,或者使用Array.Copy,如其他地方所示。

var arrOld = new byte[] { 0, 1, 2, 3, 5 };

int buffersize = 10;
var arrNew = new byte[buffersize];
Array.Copy(arrOld, arrNew, arrOld.Length);

You will need to use unsafe code in C#, which requires you to check the 'Allow unsafe code' option in the build settings. 您将需要在C#中使用不安全代码,这需要您在构建设置中选中“允许不安全代码”选项。

unsafe
{
  byte[] b = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  fixed (byte* pStart = b)
  {
    byte* pHeader = pStart + 2;
    byte* pData = pStart + 5;
  }
}

Be careful there: with your C code there was only ever one array. 小心点:在您的C代码中,只有一个数组。 Your other array is really just a view into the original. 您的其他阵列实际上只是原始视图。 If you really want two arrays, you can use Array.Copy() , like so: 如果确实需要两个数组,则可以使用Array.Copy() ,如下所示:

char[] s = {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'};
char[] b = new char[5];
Array.Copy(s, 6, b, 0, 5);

It's also worth mentioning that in c#, characters and bytes are very different things. 还值得一提的是,在c#中,字符和字节是非常不同的东西。 Don't confuse them or you'll find yourself in trouble. 不要混淆它们,否则您会发现自己遇到了麻烦。 Additionally, arrays are rarely the best option in C#. 此外,数组很少是C#中的最佳选择。 Use a class, a generic List, or even just an IEumerable instead. 改用类,通用列表或什至只是IEumerable。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM