简体   繁体   中英

C# How to write bytes into the middle of a byte[] array

The function ReadPipe() below reads chunks of bytes, and I need each chunk to go to the next location in byte[] packet_buffer. But I can't figure out how to tell.ReadPipe to write bytes to within packet_buffer.

If it was C, I could just specify: *packet_buffer[ byte index of next chunk ]

How do I do this in C#?

public static int receive_SetStreamPipe_2( byte[] packet_buffer,  int bytes_to_read )
    {
        uint received_chunk_bytes = 0;
        int remaining_bytes = bytes_to_read;
        int total_transferred_bytes = 0;


        // Use DataPipeInformation to get the actual PipeID             
        ftStatus = USB_device_selection0.SetStreamPipe( FT_pipe_information.PipeId, (UInt32)bytes_to_read );             
        if (ftStatus != FTDI.FT_STATUS.FT_OK)                 
                    return -(int)ftStatus;     // lookup:    FTDI.FT_STATUS



        // For each chunk 'o bytes:
        for(;;)
        {
            // Read chunk of bytes from FPGA:
                ftStatus = USB_device_selection0.ReadPipe( FT_pipe_information.PipeId, 
                                                            packet_buffer( remaining_bytes )  , <<<<<<<<<<<<<<  THIS WON'T WORK
                                                            (uint)remaining_bytes,
                                                            ref received_chunk_bytes );
            if (ftStatus != FTDI.FT_STATUS.FT_OK)                 
                        return -(int)ftStatus;     // lookup:    FTDI.FT_STATUS

            total_transferred_bytes +=  (int)received_chunk_bytes;
            remaining_bytes -=  (int)received_chunk_bytes;


            // Get more if not done:
            if( total_transferred_bytes <  bytes_to_read )
            {
                continue;  // go get more
            }

            return 0;
        }
    }

Based on CodeCaster's response, the best answer so far is that I have asked the FTDI company that makes the USB host driver to provide an overload with an offset.

Making the following assumption on your code, which I really shouldn't have to, please read [ask] and provide all relevant details:

  • receive_SetStreamPipe_2(byte[] packet_buffer, int bytes_to_read) :
    • Is implemented by you
    • Receives in packet_buffer an array that is at least bytes_to_read long and needs to be filled with exactly bytes_to_read bytes
  • USB_device_selection0.ReadPipe(FT_pipe_information.PipeId, packet_buffer, (uint)remaining_bytes, ref received_chunk_bytes) :
    • Fills packet_buffer from index 0 and doesn't have an overload with an offset (such as Stream.Write(buffer, offset, count) )
    • Fills it up to at most remaining_bytes , but probably less
    • Assigns the received_chunk_bytes to how many bytes have been read

Then you need to introduce a temporary buffer that you copy to the final buffer. How large that buffer should optimally be should be obtainable from the API information, but let's take 1024 bytes:

uint received_chunk_bytes = 0;
int remaining_bytes = bytes_to_read;
int total_transferred_bytes = 0;

// Create a smaller buffer to hold each chunk
int chunkSize = 1024;
byte[] chunkBuffer = new byte[chunkSize];

// ...

for (;;)
{
    // Read chunk of bytes from FPGA into chunkBuffer, chunk size being the the buffer size or the remaining number of bytes, whichever is less
    ftStatus = USB_device_selection0.ReadPipe(FT_pipe_information.PipeId, 
                                              chunkBuffer
                                              (uint)Math.Min(chunkSize, remaining_bytes),
                                              ref received_chunk_bytes);

    if (ftStatus != FTDI.FT_STATUS.FT_OK)                 
                    return -(int)ftStatus;     // lookup:    FTDI.FT_STATUS

    // Copy the chunk into the output array
    Array.Copy(chunkBuffer, 0, packet_buffer, total_transferred_bytes, received_chunk_bytes);

    total_transferred_bytes +=  (int)received_chunk_bytes;
    remaining_bytes -=  (int)received_chunk_bytes;

    // ...

If it was C, I could just specify: *packet_buffer[ byte index of next chunk ]

You know what a Buffer Overflow error is? Because that is how we get buffer Overlow errors! Do not write past the memory you assigned for the array, or not even god knows what you are overwriting there!

In C# - as most other langauges - Arrays are fixed to the size you gave when creating them. Unlike many other languages C# (well actually .NET) will not allow you to accidentally cause a buffer overflow by writing past that. There is a sanity check on every Index given, just so nobody messes things up.

If you want a array that Automagicalyl grows and allows for random inserts, List<byte> is the droid you are looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM