十六进制字符和哈希值的错误

时间:2015-01-02 03:49:24

标签: c# hex

我有一个项目几乎接近完成,除了一些顽固但可能很简单的错误我接收。据我所知,我知道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);

    }
}
}

2 个答案:

答案 0 :(得分:4)

x(y)表示&#34;以x作为参数调用y&#34;。

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

也许您打算用"0123456789ABCDEF"[buf]对其进行索引?这将返回来自&#34; 0123456789ABCDEF&#34;的buf&#39;字符,只要buf在0到15之间,就会以十六进制为buf

答案 1 :(得分:0)

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

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