我有一个byte[],我正在遍历int(和其他数据)列表,并且我想将int复制到我的byteArray [索引* 4]中。我该怎么做?
BitConverter
可能是你的好朋友。
然而,它通常会返回一个新的字节数组。它也不允许你指定字节序。我在MiscUtil中有一个EndianBitConverter
类,它具有通过将数据直接复制到现有字节数组中来转换基元类型的方法。
例如:
// Copy the bytes from the integer "value" into a byte array
// (buffer) starting at index 5
EndianBitConverter.Little.CopyBytes(value, buffer, 5);
按照BinaryWriter的方式去做:
buffer[0] = (byte) value;
buffer[1] = (byte) (value >> 8);
buffer[2] = (byte) (value >> 0x10);
buffer[3] = (byte) (value >> 0x18);
(显然,这将把整数复制到数组的前4个字节中)
步骤2。 你可以找到BitConverter.GetBytes(int value)方法的源代码:BitConverter 可能是你的好朋友。
然而,它通常会返回一个新的字节数组。
public static unsafe byte[] GetBytes(int value)
{
byte[] numArray = new byte[4];
fixed (byte* numPtr = numArray)
*(int*) numPtr = value;
return numArray;
}
第三步。运用想象力并更改第二步中的代码几行,使其能够将数据保存到现有数组中:public static unsafe byte[] GetBytes(int value, int buffer[], int offset)
{
// Here should be a range check. For example:
// if (offset > buffer.Length + sizeof(int)) throw new IndexOutOfRangeException();
fixed (byte* numPtr = &buffer[offset])
*(int*) numPtr = value;
}
有很多方法可以实现,但为了使其更好,请使用c#的一个新功能:扩展。
32位整数需要4个字节,因此在byte[]中占用4个位置。如何将整数分解为其4个组成字节?您可以使用位操作符 >> 进行分解。该运算符将整数中的位向右移动指定数量的位。例如:
integer = 10399
binary = 00101000 10011111
10399 >> 1 == 0010100 01001111 each bit shifted by 1
一个字节由8位组成,如果我们将整数向左移动8位,则整数的新第二个字节值将出现在最右边的位位置。
10399 >> 8 = 00000000 00101000
(byte)(10399 >> 8) == 0010100
因此,使用这种技术来处理4个字节将使我们能够访问每个字节,并将它们复制到传递给我们的新CopyToByteArray方法的目标数组中:
public static class Int32Extension
{
public static void CopyToByteArray(this int source, byte[] destination, int offset)
{
if (destination == null)
throw new ArgumentException("Destination array cannot be null");
// check if there is enough space for all the 4 bytes we will copy
if (destination.Length < offset + 4)
throw new ArgumentException("Not enough room in the destination array");
destination[offset] = (byte)(source >> 24); // fourth byte
destination[offset+1] = (byte)(source >> 16); // third byte
destination[offset+2] = (byte)(source >> 8 ); // second byte
destination[offset+3] = (byte)source; // last byte is already in proper position
}
}
int something = 20;
byte[] array = new byte[4];
something.CopyToByteArray(array,0);
2022年更新
自提出该问题以来已经过去了几年。如果今天有人在寻找相关信息,可以使用.NET 6中更简单的版本。
使用BinaryPrimitives
类
将int或其他基本数据类型写入数组中。
BinaryPrimitives.WriteInt32LittleEndian(array, myInt);
当然,还有许多方法可以编写不同的原语作为大端或小端
byte[] bytes = new byte[listOfInts.Count * sizeof(int)];
int pos = 0;
foreach(int i in listOfInts)
{
byte[] b = BitConverter.GetBytes(i);
b.CopyTo(bytes, pos);
pos += b.Length;
}
Buffer.BlockCopy(intArray, 0, byteArray, 0, 4*intArray.Length)
该方法用于在两个数组之间复制数据。最后一个参数表示要复制的数据量(以字节为单位)。
概括ADB的精美答案:
public static class Int32Extension
{
/// <summary>
/// Copies an int to a byte array: Byte order and sift order are inverted.
/// </summary>
/// <param name="source">The integer to convert.</param>
/// <param name="destination">An arbitrary array of bytes.</param>
/// <param name="offset">Offset into the desination array.</param>
public static void CopyToByteArray(this int source, byte[] destination, int offset)
{
if (destination == null)
throw new ArgumentException("Destination array cannot be null");
// check if there is enough space for all the 4 bytes we will copy
if (destination.Length < offset + sizeof(int))
throw new ArgumentException("Not enough room in the destination array");
for (int i = 0, j = sizeof(int) - 1; i < sizeof(int); i++, --j) {
destination[offset + i] = (byte) (source >> (8 * j));
}
}
/// <summary>
/// Copies an int to a to byte array Little Endian: Byte order and sift order are the same.
/// </summary>
/// <param name="source">The integer to convert.</param>
/// <param name="destination">An arbitrary array of bytes.</param>
/// <param name="offset">Offset into the desination array.</param>
public static void CopyToByteArrayLE(this int source, byte[] destination, int offset)
{
if (destination == null)
throw new ArgumentException("Destination array cannot be null");
// check if there is enough space for all the 4 bytes we will copy
if (destination.Length < offset + sizeof(int))
throw new ArgumentException("Not enough room in the destination array");
for (int i = 0, j = 0; i < sizeof(int); i++, j++) {
destination[offset + i] = (byte) (source >> (8 * j));
}
}
}