十六进制字符和哈希错误

本文关键字:错误 哈希 字符 十六进制 | 更新日期: 2023-09-27 18:35:48

我有一个几乎接近完成的项目,除了我收到的几个顽固但可能简单的错误。我知道这意味着对C的了解,我能把这个项目带到这一步是一个奇迹。我希望有人能检测到我的代码中缺少什么。这是错误的视图,下面是代码。

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)的意思是"以y作为参数调用x"。

你已经写了"0123456789ABCDEF"(buf). "0123456789ABCDEF"不是函数(或函子),因此您无法调用它。

也许你的意思是用"0123456789ABCDEF"[buf]索引它?这将返回"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;
}