十六进制字符和散列错误

Errors on hex characters and hash

除了我收到的几个顽固但可能很简单的错误外,我有一个几乎接近完成的项目。据了解,我对 C 了如指掌,我能把这个项目做到这么远真是一个奇迹。我希望有人能检测到我的代码中缺少什么。 Here 是错误的视图,下面是代码。

private void button1_Click(object sender, EventArgs e)
    {

        Random rnd = new Random();
        StringBuilder bin = new StringBuilder();
        int buf = 0;
        int bufLen = 0;
        int left = 53;
        for (int i = 106; i >= 1; i += -1)
        {
            buf <<= 1;
            if (rnd.Next(i) < left)
            {
                buf += 1;
                left -= 1;
            }
            bufLen += 1;
            if (bufLen == 4)
            {
                bin.Append("0123456789ABCDEF"(buf));
                bufLen = 0;
                buf = 0;
            }
        }
        string b = bin.ToString();
        bin.Append("048c"(buf));


        System.Security.Cryptography.SHA1Managed m = new System.Security.Cryptography.SHA1Managed();
        byte[] hash = m.ComputeHash(Encoding.UTF8.GetBytes(b));

        //replace first two bits in hash with bits from buf
        hash(0) = Convert.ToByte(hash(0) & 0x3f | (buf * 64));
        //append 24 bits from hash
        b = b.Substring(0, 26) + BitConverter.ToString(hash, 0, 3).Replace("-", string.Empty);

    }
}
}

x(y) 表示 "call x with y as a parameter".

你写了"0123456789ABCDEF"(buf)"0123456789ABCDEF" 不是函数(或仿函数),因此您不能调用它。

也许你想用 "0123456789ABCDEF"[buf] 索引它?此 returns 来自“0123456789ABCDEF”的第 buf 个字符,只要 buf 介于 0 和 15 之间,它就是十六进制的 buf

您不能将字符串文字与字符串变量连接起来。

#include <iostream>

using std::cout;

void concatenate(const std::string& s)
{
    cout << "In concatenate, string passed is: "
         << s
         << "\n";
}

int main(void)
{
    std::string world = " World!\n";
    concatenate("Hello"(world));
    return 0;
}

Thomas@HastaLaVista ~/concatenation
# g++ -o main.exe main.cpp
main.cpp: In function `int main()':
**main.cpp:15: error: `"Hello"' cannot be used as a function**

Thomas@HastaLaVista ~/concatenation
# g++ --version
g++ (GCC) 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

您将需要一个临时字符串变量:

if (bufLen == 4)
{
   std::string temp("01234567890ABCDEF");
   temp += buf;
   bin.Append(temp);
   bufLen = 0;
   buf = 0;
}