gpt4 book ai didi

c# - C#4个byte []数组到int []的索引

转载 作者:太空宇宙 更新时间:2023-11-03 17:48:14 26 4
gpt4 key购买 nike

我有一个字节数组

 byte[0]=00;
byte[1]=01;
byte[2]=02;
byte[3]=03;
...


我想按顺序获得四个字节ex)00010203
并将它们转换为int32数组,因此int [0]将为66051

int[] result = new int[byteArray.Length / 4]; 
Buffer.BlockCopy(byteArray, 0, result, 0, byteArray.Length);


我尝试使用上面的代码,但结果却大不相同。

最佳答案

作为不安全的替代方法,您可以尝试以下操作:

public static int[] GetIntsUnsafe(byte[] bytes)
{
int numBytes = bytes.Length;
int numInts = numBytes / 4;
if (numBytes / 4d != numInts) throw new Exception();
if (numInts == 0) return new int[0];

var ints = new int[numInts];

unsafe
{
fixed (byte* pBytes = bytes)
fixed (int* pInts = ints)
{
byte* rawInt = (byte*)pInts;

for (int i = 0; i < numBytes; i += 4)
{
for (int j = 0; j < 4; j++)
{
rawInt[i + j] = pBytes[3 - j + i];
}
}

}
}

return ints;
}


我在Release,AnyCPU中对三个答案进行了性能测试(在撰写本文时),没有附带调试器以获取:

Unsafe Small: 22
Unsafe Large: 5
John Small: 816
John Large: 23628
General Small: 49 // 266 with Linq ToArray
General Large 4 // 29 with Linq ToArray


测试代码:

Random r = new Random(0);
var byteSmall = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x03, 0x05, 0x07, 0x23 };
var byteLarge = new byte[10000];
for (int i = 0; i < byteLarge.Length; i++) byteLarge[i] = (byte)r.Next(255);

int countSmall = 2_000_000;
int countLarge = 1_000;

Stopwatch swUnsafeSmall = Stopwatch.StartNew();
for (int i = 0; i < countSmall; i++) GetIntsUnsafe(byteSmall);
swUnsafeSmall.Stop();

Console.WriteLine("Unsafe Small: " + swUnsafeSmall.ElapsedMilliseconds);

Stopwatch swUnsafeLarge = Stopwatch.StartNew();
for (int i = 0; i < countLarge; i++) GetIntsUnsafe(byteLarge);
swUnsafeLarge.Stop();

Console.WriteLine("Unsafe Large: " + swUnsafeLarge.ElapsedMilliseconds);

Stopwatch swJohnSmall = Stopwatch.StartNew();
for(int i = 0; i < countSmall;i++)
GetIntsJohn(byteSmall);
swJohnSmall.Stop();

Console.WriteLine("John Small: " + swJohnSmall.ElapsedMilliseconds);

Stopwatch swJohnLarge = Stopwatch.StartNew();
for(int i = 0; i < countLarge;i++)
GetIntsJohn(byteLarge);
swJohnLarge.Stop();

Console.WriteLine("John Large: " + swJohnLarge.ElapsedMilliseconds);


Stopwatch swGeneralSmall = Stopwatch.StartNew();
for(int i = 0; i < countSmall;i++)
GetIntsGeneral(byteSmall);
swGeneralSmall.Stop();

Console.WriteLine("General Small: " + swGeneralSmall.ElapsedMilliseconds);

Stopwatch swGeneralLarge = Stopwatch.StartNew();
for(int i = 0; i < countLarge;i++)
GetIntsGeneral(byteLarge);
swGeneralLarge.Stop();

Console.WriteLine("General Large: " + swGeneralLarge.ElapsedMilliseconds);


Console.ReadLine();


我的版本是John和General的代码。 (我编辑了TheGeneral的代码以不复制数组):

public static int[] GetIntsJohn(byte[] byteArray)
{
int[] result = new int[byteArray.Length / 4];
for (int i = 0; i < result.Length; ++i)
{
var srcBytes = byteArray.Skip(i * 4).Take(4);
if (BitConverter.IsLittleEndian)
{
srcBytes = srcBytes.Reverse();
}
result[i] = BitConverter.ToInt32(srcBytes.ToArray(), 0);
}

return result;
}



public static uint SwapBytes(uint x)
{
// swap adjacent 16-bit blocks
x = (x >> 16) | (x << 16);
// swap adjacent 8-bit blocks
return ((x & 0xFF00FF00) >> 8) | ((x & 0x00FF00FF) << 8);
}

public static int[] GetIntsGeneral(byte[] source)
{
var result = new int[source.Length / 4];
Buffer.BlockCopy(source, 0, result, 0, source.Length);
for(int i= 0;i<result.Length;i++)
{
result[i] = (int) SwapBytes((uint) result[i]);
}

return result;
}

关于c# - C#4个byte []数组到int []的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51257449/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com