BlockCopy

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Urs Wigger

    #1

    BlockCopy

    From a socket, I read some data into a bytes buffer.

    byte[] bytes = new byte[1024];
    int headSize = 8;
    int nRead = sock.Receive(by tes, 0, headSize, SocketFlags.Pee k);

    Next I would like to 'mask' the received data with a struct 'HeaderT'

    public struct HeaderT
    {
    int packetSize;
    int type;
    };

    In C++ this could be done similar to this:
    HeaderT *pHead = (HeaderT *)bytes;

    In C#, this obviously does not work, so I tried with BlockCopy():

    HeaderT header = new HeaderT();
    System.Buffer.B lockCopy(bytes, 0, header , 0, headSize);

    But then I get the following syntax error
    Argument '3': cannot convert from 'HeaderT' to 'System.Array'.

    So far I found no solution. Any help would be appreciated
    Regards Urs



  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: BlockCopy

    Urs,

    You will have to pass an array of type HeaderT into the function, with
    one element in it.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Urs Wigger" <ns_urs.wigger@ leica-geosystems_ns.c om> wrote in message
    news:Od6Za3qrDH A.732@TK2MSFTNG P09.phx.gbl...[color=blue]
    > From a socket, I read some data into a bytes buffer.
    >
    > byte[] bytes = new byte[1024];
    > int headSize = 8;
    > int nRead = sock.Receive(by tes, 0, headSize, SocketFlags.Pee k);
    >
    > Next I would like to 'mask' the received data with a struct 'HeaderT'
    >
    > public struct HeaderT
    > {
    > int packetSize;
    > int type;
    > };
    >
    > In C++ this could be done similar to this:
    > HeaderT *pHead = (HeaderT *)bytes;
    >
    > In C#, this obviously does not work, so I tried with BlockCopy():
    >
    > HeaderT header = new HeaderT();
    > System.Buffer.B lockCopy(bytes, 0, header , 0, headSize);
    >
    > But then I get the following syntax error
    > Argument '3': cannot convert from 'HeaderT' to 'System.Array'.
    >
    > So far I found no solution. Any help would be appreciated
    > Regards Urs
    >
    >
    >[/color]


    Comment

    • Mattias Sjögren

      #3
      Re: BlockCopy

      [color=blue]
      > You will have to pass an array of type HeaderT into the function, with
      >one element in it.[/color]

      I don't think BlockCopy works with UDTs, only with the primitive
      types. So I'd do something like this instead

      HeaderT header;
      header.packetSi ze = BitConverter.To Int32( bytes, 0 );
      header.type = BitConverter.To Int32( bytes, 4 );

      [color=blue][color=green]
      >> In C++ this could be done similar to this:
      >> HeaderT *pHead = (HeaderT *)bytes;
      >>
      >> In C#, this obviously does not work[/color][/color]

      Actually you can, in unsafe code. But you may not want to do that.



      Mattias

      --
      Mattias Sjögren [MVP] mattias @ mvps.org

      Please reply only to the newsgroup.

      Comment

      Working...