【问题标题】:Convert function with byte[] from C# to C++ [closed]将带有字节 [] 的函数从 C# 转换为 C++ [关闭]
【发布时间】:2017-12-07 09:16:04
【问题描述】:

我的函数有原始 C# 代码: 它们就像一个魅力,我会将它们转换为 C++

    public static byte[] EncryptBlock(byte[] filebuffer)
    {
        int a, i, j, k, tmp;
        int[] key, box;
        byte[] cipher;

        key = new int[KeyBoxLength];
        box = new int[KeyBoxLength];
        cipher = new byte[filebuffer.Length];

        for (i = 0; i < KeyBoxLength; i++)
        {
            key[i] = XorContentLock[i % XorContentLock.Length];
            box[i] = i;
        }
        for (j = i = 0; i < KeyBoxLength; i++)
        {
            j = (j + box[i] + key[i]) % 256;
            tmp = box[i];
            box[i] = box[j];
            box[j] = tmp;
        }
        for (a = j = i = 0; i < filebuffer.Length; i++)
        {
            a++;
            a %= KeyBoxLength;
            j += box[a];
            j %= KeyBoxLength;
            tmp = box[a];
            box[a] = box[j];
            box[j] = tmp;
            k = box[((box[a] + box[j]) % KeyBoxLength)];
            cipher[i] = (byte)(filebuffer[i] ^ k);
        }
        return cipher;
    }

这就是它们在 C++ 上的外观,但我不知道为什么会出现这些错误。

std::string Conversion::EncryptBlock(std::string& buffer)
{
    int32 a, i, j, k, tmp;
    int key[256];
    int box[256];
    BYTE* cipher = new BYTE[buffer.length];
    for (i = 0; i < KeyBoxLength; i++)
    {
        key[i] = XorFileLock[i % 16];
        box[i] = i;
    }
    for (j = i = 0; i < KeyBoxLength; i++)
    {
        j = (j + box[i] + key[i]) % 256;
        tmp = box[i];
        box[i] = box[j];
        box[j] = tmp;
    }
    for (a = j = i = 0; i < buffer.length; i++)
    {
        a++;
        a %= KeyBoxLength;
        j += box[a];
        j %= KeyBoxLength;
        tmp = box[a];
        box[a] = box[j];
        box[j] = tmp;
        k = box[((box[a] + box[j]) % KeyBoxLength)];
        cipher[i] = (byte)(buffer[i] ^ k);
    }
    std::string ret(reinterpret_cast< char const* >(cipher));
    delete[] cipher;

    return ret;
}

当我编译它时,我得到了这个:

我已经得到了一些关于如何转换它们并动态分配字节数组的文章。但就我而言,它似乎不起作用,我无法理解如何将那些工作的 C# 代码转换为 C++。不知道如何将 C# byte[] 解析为 unsigned* int C++,然后将这些数组转换为 std::string。

好吧,我在那里做错了什么?请解释一下,为什么?它应该是怎样的? 另一个例子:

【问题讨论】:

  • 我已经阅读了这些内容并尝试过,但它们告诉我“表达式必须具有恒定值”。我完全失望了。
  • @EamonnMcEvoy C++ 没有可变长度数组。它确实有std::vector
  • 上面的代码,“应该”有效,因为我看不到任何错误(我所做的和教程上的一样),而且,IntelliTrace 上没有任何东西,没有任何红线。只是简单不编译...屏幕上方。不知道如何转换这些...

标签: c# c++ arrays


【解决方案1】:

std::string::length 是一个方法,而不是一个常量。您看到的错误是告诉您无法从“指向std::string 的成员函数的指针”转换为unsigned int。您缺少一对括号:

BYTE* cipher = new BYTE[buffer.length()];
                                  // ^^ Here

与您的问题无关,您应该使用std::vector&lt;BYTE&gt; 而不是管理您自己的原始数组。您可以将cipher的声明更改为

std::vector<BYTE> cipher(buffer.length());

并将ret的声明改为

std::string ret(cipher.begin(), cipher.end());

那你就不用记得delete[] cipher了。虽然没有真正的理由将cipher 复制到ret,但您可以直接将cipher 声明为std::string cipher(buffer.length(), '\0');return cipher;

【讨论】:

  • 哦,谢谢你的解释!
  • @HappyDay 如果答案帮助您解决了您的问题/错误,请将其标记为正确答案,以便阅读您的问题或遇到相同问题的其他人知道在哪里查找。
猜你喜欢
  • 2020-08-26
  • 2015-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-19
  • 1970-01-01
相关资源
最近更新 更多