c# - Function C++ 到 C#(安全代码)

标签 c# c++ arrays unsafe

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#: ?

最佳答案

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);
    }
}

关于c# - Function C++ 到 C#(安全代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3435586/

相关文章:

c# - 返回键的Key Down事件不起作用

c# - LINQ 到 XML : How to select the next element

c++ - 如何在我的程序中创建日志系统?

c++ - 为什么string和int之间的+运算符充当substr

c# - Base64 编码和序列化数组 WHMCS

c# - ElasticSearch NEST API 将值更新为空

c++ - 并行化 for 循环不会带来性能提升

javascript - 将来自 JavaScript 对象中不同属性的数组项组合起来

java - 为什么会出现java.lang.ArrayIndexOutOfBoundsException?

Java:如何拥有子类类型的数组?