函数c++到c#(安全代码)

时间:2022-09-01 09:51:42

c++:

c++:

static void doIp(byte data[])
{
  unsigned char j, k;
  byte val;
  byte buf[8];
  byte *p;
  byte i = 8;

  for(i=0; i<8; i++)
  {
    val = data[i];
    p = &buf[3];
    j = 4;

    do
    {
      for(k=0; k<=4; k+=4)
      {
        p[k] >>= 1;
        if(val & 1) p[k] |= 0x80;
        val >>= 1;
      }
      p--;
    } while(--j);
  }

  memcpy(data, buf, 8);
}

c#: ?

c#:?

1 个解决方案

#1


5  

class someclass {
    public static void doIp(byte[] data)
    {
        uint j, k; // these are just counters, so uint is fine
        byte val;
        byte[] buf = new byte[8];  // syntax changed here
        byte p;                    // you end up using p simply as an offset from buf
        byte i = 8;

        for(i=0; i<8; i++)
        {
            val = data[i];
            p = 3;
            j = 4;

            do
            {
                for(k=0; k<=4; k+=4)
                {
                    buf[p+k] >>= 1;
                    if((val & 1) != 0) buf[p+k] |= 0x80; // must test against 0 explicitly in C#
                    val >>= 1;
                }
                p--;
            } while(--j != 0); // must test against 0 explicitly in C#
        }

        Array.Copy(buf, data, 8);
    }
}

#1


5  

class someclass {
    public static void doIp(byte[] data)
    {
        uint j, k; // these are just counters, so uint is fine
        byte val;
        byte[] buf = new byte[8];  // syntax changed here
        byte p;                    // you end up using p simply as an offset from buf
        byte i = 8;

        for(i=0; i<8; i++)
        {
            val = data[i];
            p = 3;
            j = 4;

            do
            {
                for(k=0; k<=4; k+=4)
                {
                    buf[p+k] >>= 1;
                    if((val & 1) != 0) buf[p+k] |= 0x80; // must test against 0 explicitly in C#
                    val >>= 1;
                }
                p--;
            } while(--j != 0); // must test against 0 explicitly in C#
        }

        Array.Copy(buf, data, 8);
    }
}