memcpy like operation..

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

    #1

    memcpy like operation..

    e.g.
    byte[] z = new byte[255];
    int a = 10;
    int b= 20;

    I wanna do sth like

    memcpy(&z[10], &a, sizeof(a));

    how can I do it in c#?
    do I need to do things like

    byte[] temp = System.BitConve rter.GetBytes(a );
    for (int i=0;i<temp.Leng th;i++
    z[10+i] = temp[i];

    thx....


  • Dmitriy Lapshin [C# / .NET MVP]

    #2
    Re: memcpy like operation..

    Hi,

    This seems to be the .NET way of doing it as you embed no knowledge of how
    an integer is actually represented by a sequence of bytes into your code -
    the BitConverter class does this for you and should this change in the next
    version of the framework, you're on the safe side.

    --
    Dmitriy Lapshin [C# / .NET MVP]
    X-Unity Test Studio

    Bring the power of unit testing to VS .NET IDE

    "babylon" <amuro@hotmail. com> wrote in message
    news:OlxaQO6lDH A.644@TK2MSFTNG P11.phx.gbl...[color=blue]
    > e.g.
    > byte[] z = new byte[255];
    > int a = 10;
    > int b= 20;
    >
    > I wanna do sth like
    >
    > memcpy(&z[10], &a, sizeof(a));
    >
    > how can I do it in c#?
    > do I need to do things like
    >
    > byte[] temp = System.BitConve rter.GetBytes(a );
    > for (int i=0;i<temp.Leng th;i++
    > z[10+i] = temp[i];
    >
    > thx....
    >
    >[/color]

    Comment

    • William Stacey

      #3
      Re: memcpy like operation..

      See http://www.csharp-corner.com and look for the simple sniffer example.
      It has some ideas for you using pointers.

      --
      William Stacey, MVP

      "babylon" <amuro@hotmail. com> wrote in message
      news:OlxaQO6lDH A.644@TK2MSFTNG P11.phx.gbl...[color=blue]
      > e.g.
      > byte[] z = new byte[255];
      > int a = 10;
      > int b= 20;
      >
      > I wanna do sth like
      >
      > memcpy(&z[10], &a, sizeof(a));
      >
      > how can I do it in c#?
      > do I need to do things like
      >
      > byte[] temp = System.BitConve rter.GetBytes(a );
      > for (int i=0;i<temp.Leng th;i++
      > z[10+i] = temp[i];
      >
      > thx....
      >
      >[/color]


      Comment

      • Till Meyer

        #4
        Re: memcpy like operation..

        On Tue, 21 Oct 2003 15:19:12 +0800, babylon wrote:
        [color=blue]
        > e.g.
        > byte[] z = new byte[255];
        > int a = 10;
        > int b= 20;
        >
        > I wanna do sth like
        >
        > memcpy(&z[10], &a, sizeof(a));[/color]

        System.BitConve rter.GetBytes(a ).CopyTo(z,10);

        Till Meyer

        Comment

        Working...