CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2004
    Posts
    429

    Question Isolate valid Data in byte[] [C#]

    --Using [VS C#.NET]--

    Given a byte array of size 64 (baBuff[64]), this is used as a buffer to store incomming data.
    Most of the time the buffers capacity is not completly used, however I know the exact length of valid data in the buffer [int DataPointer = length of valid Data]

    Thing is I am trying to copy the valid data in my baBuff into a larger byte[] message I am creating, I don't want to be stuck coping over the entire 64bytes everytime.
    Currently I am using the following code:

    :: CODE ::

    byte[] baTemp = new byte[DataPointer];
    for (int i=0; i<nLen; i++)
    {
    baTemp[i] = baBuff[i];
    }

    // Load Raw Data
    baBuff.CopyTo(baData, 5);


    Is there a better way to do this? Can I access the byte[] using something like substring?
    There has to be a nicer method... any clues?

  2. #2
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Isolate valid Data in byte[] [C#]

    Firstly, CopyTo isn't the most efficient way of doing this. CopyTo is a member of Array and has loads of type checking etc in it. You're better off using Buffer.BlockCopy to do this.

    And no, if you're copying from a small array to a large array there's no way of doing this other than using BlockCopy.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured