Simplest way to copy int to byte[]

2019-04-27 03:00发布

I have a byte[] and i am iterating through a list of ints(and other data) and i want to copy the int to my byteArray[index*4] How do i do this?

标签: c# bytearray
7条回答
我命由我不由天
2楼-- · 2019-04-27 03:25

There are many different ways to do it, but to make it nicer, lets use a new feature of c#: extensions.

An integer of the 32 bit variety takes 4 bytes, so it will occupy 4 spots in the byte[]. How do you break an integer in its 4 constituent bytes? You can do it using the bit manipulation operator >>. That operator shifts the bits in the integer by the specified number of bits. For example:

integer = 10399
binary = 00101000 10011111
10399 >> 1 == 0010100 01001111 each bit shifted by 1

Since a byte is 8 bits if we shift the integer by 8 bits we will have the new second byte value of the integer in the right-most bits position

10399 >> 8 = 00000000 00101000

Notice how the second byte is not the first byte and the rest of the bits have been replaced by 0.

We can use this trick to move the bytes into the first position, then force a cast to byte, which will discard the 3 other bytes and will leave us the last bytes value:

(byte)(10399 >> 8) == 0010100

So, using this technique for the 4 bytes will give us access to each one and we will copy them in a destination array that was passed to our new CopyToByteArray method:

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
    }
}

Note that we could have copied the bytes in reverse order, that is up to your implementation.

The extension function will allow you to access the new function as a member of the integer class:

int something = 20;
byte[] array = new byte[4];
something.CopyToByteArray(array,0);
查看更多
倾城 Initia
3楼-- · 2019-04-27 03:28
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;
}
查看更多
Fickle 薄情
4楼-- · 2019-04-27 03:31

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

Copies data between two arrays. The last argument is the amount of data to copy in bytes.

查看更多
女痞
5楼-- · 2019-04-27 03:43

Do it how the BinaryWriter does it:

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

(obviously this will copy the int into the first 4 bytes of the array)

查看更多
萌系小妹纸
6楼-- · 2019-04-27 03:44

I'll try to summarize some of the previous answers to make something new

Step 1. As Jon Skeet said before:

BitConverter is quite possibly your friend.

However, that generally returns you a new byte array.

Step 2. You can find the source code of BitConverter.GetBytes(int value) method:

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

Step 3. Using imagination and changing a few lines in the code from step 2 so it could save the data to an existing array:

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;
}
查看更多
趁早两清
7楼-- · 2019-04-27 03:52

BitConverter is quite possibly your friend.

However, that generally returns you a new byte array. It also doesn't let you specify endianness. I have the EndianBitConverter class in MiscUtil which has methods to convert primitive types by copying the data directly into an existing byte array.

For instance:

// Copy the bytes from the integer "value" into a byte array
// (buffer) starting at index 5
EndianBitConverter.Little.CopyBytes(value, buffer, 5);
查看更多
登录 后发表回答