11

我有一个 byte[],我正在遍历一个 int(和其他数据)列表,我想将 int 复制到我的 byteArray[index*4] 我该怎么做?

4

7 回答 7

21

BitConverter很可能是你的朋友。

但是,这通常会返回一个的字节数组。它也不允许您指定字节顺序。我在MiscUtilEndianBitConverter中有一个类,它具有通过将数据直接复制到现有字节数组中来转换原始类型的方法。

例如:

// Copy the bytes from the integer "value" into a byte array
// (buffer) starting at index 5
EndianBitConverter.Little.CopyBytes(value, buffer, 5);
于 2009-08-17T10:08:32.450 回答
10

按照 BinaryWriter 的方式进行操作:

buffer[0] = (byte) value;
buffer[1] = (byte) (value >> 8);
buffer[2] = (byte) (value >> 0x10);
buffer[3] = (byte) (value >> 0x18);

(显然这会将 int 复制到数组的前 4 个字节中)

于 2009-08-17T10:09:30.700 回答
7

我将尝试总结一些以前的答案,以创造新的东西

第 1 步。正如Jon Skeet之前所说:

BitConverter很可能是您的朋友。

但是,这通常会返回一个的字节数组。

步骤 2.您可以找到 BitConverter.GetBytes(int value) 方法的源代码:

public static unsafe byte[] GetBytes(int value)
{
    byte[] numArray = new byte[4];
    fixed (byte* numPtr = numArray)
        *(int*) numPtr = value;
    return numArray;
}

步骤 3.使用想象力并更改步骤 2 中的代码中的几行,以便可以将数据保存到现有数组中:

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;
}
于 2012-11-24T20:50:04.617 回答
6

有很多不同的方法可以做到这一点,但为了让它更好,让我们使用 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

请注意第二个字节不是第一个字节,其余位已被 0 替换。

我们可以使用这个技巧将字节移动到第一个位置,然后强制转换为字节,这将丢弃其他 3 个字节并留给我们最后一个字节值:

(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);
于 2009-08-17T10:30:08.147 回答
1
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;
}
于 2009-08-17T10:13:00.007 回答
1

Buffer.BlockCopy(intArray, 0, byteArray, 0, 4*intArray.Length)

在两个数组之间复制数据。最后一个参数是要复制的数据量(以字节为单位)。

于 2009-08-17T10:20:45.607 回答
0

概括亚行的精妙答案:

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));
        }
    }
}
于 2014-11-16T21:12:00.590 回答