How to port C# Buffer.BlockCopy to Python 3

40 Views Asked by At

I've been trying to implement a working solution to mimic C#'s Buffer.BlockCopy() function in Python 3, I can't seem to get it working and I've tried a ton of resources online to try and help with my problem but to no avail.

I want to copy an array of unsigned 32bit integers (of size 4) that is returned by a function to a new byte array (of size 16), I hope I've explained it correctly!

So far I have tried the following and have not accomplished the same result as C#'s Buffer.BlockCopy() function.

    array = [None] * 4
    array2 = [None] * 16
    array = some_function() # The function with return an array of 4 unsigned 32bit integers
    pos = 0
    array2[pos:pos + 16] = array

The result of the above code is just a basic copy from array -> array2 (array2 is now of size 4 not 16)

The expected result of array2 was an array of bytes not integers

Example:

I would expect the elements in array2 to be: (This is what C# outputs from Buffer.BlockCopy()) 122, 90, 207, 41, 176, 84, 192, 48, 224, 6, 213, 250, 129, 69, 52, 23

After copying array whose contents are: 701454970, 817910960, 4208264928, 389301633

Additional information:

This is the description of C#'s Buffer.BlockCopy() function

Copies a specified number of bytes from a source array starting at a particular offset to a destination array starting at a particular offset.

    // Copies from one primitive array to another primitive array without
    // respecting types.  This calls memmove internally.  The count and
    // offset parameters here are in bytes.  If you want to use traditional
    // array element indices and counts, use Array.Copy.
    BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count)
0

There are 0 best solutions below