如何在 ListBox 中显示字符串的散列值?
How do I show the hash of a string in a ListBox?
我有一个项目,我必须创建自己的线性哈希、桶哈希和二次哈希算法,所以我选择对一些字符串进行哈希处理,但我 运行 遇到了一个小问题2 个列表框:
- 一个充满字符串的列表框
- 一个充满这些字符串的散列的列表框
我可以毫无问题地打印字符串,但是我不知道如何在单击按钮时将它们相应的散列打印到另一个 ListBox 中。
这是我的代码:
Form.cs
private void btnLinearHash_Click(object sender, EventArgs e)
{
// Initialize Hash.cs (see below for code)
Hash.LinearHash linearHash = new Hash.LinearHash();
RandomArrayGen randGen = new RandomArrayGen();
// Clear both ListBoxes
lbHashStrings.Items.Clear();
lbHashesOfStrings.Items.Clear();
for (int i = 0; i < 12; i++)
{
// Generate 12 random upper and lowercase strings using RandomArrayGen.cs (see https://pastebin.com/jRsG5r1E)
lbHashStrings.Items.Add(randGen.GenRandomStrings(12, true, true));
}
for (int i = 0; i < lbHashStrings.Items.Count; i++)
{
// Generate a hash of a string and store it into a Dictionary (see Hash.cs)
linearHash.GenerateHashOfString(lbHashStrings.Items[i].ToString());
lbHashesOfStrings.Items.Add(linearHash.FindHashCodeInDictionary(lbHashStrings.Items[i].ToString()));
}
}
Hash.cs
public class LinearHash
{
private Dictionary<byte[], string> linearHashArray = new Dictionary<byte[], string>();
private byte[] hashCode;
public void GenerateHashOfString(string stringToHash)
{
hashCode = Encoding.ASCII.GetBytes(stringToHash);
linearHashArray.Add(hashCode, stringToHash);
}
public string FindHashCodeInDictionary(string StringToFind)
{
byte[] HashOfStringToFind = Encoding.ASCII.GetBytes(StringToFind);
//For some reason, this keeps returning false
foreach (var keyPairValue in linearHashArray)
{
if ((linearHashArray.ContainsKey(HashOfStringToFind) && linearHashArray.ContainsValue(StringToFind))
{
return Encoding.UTF8.GetString(HashOfStringToFind);
}
}
return "Hash not found";
}
}
所以,我的问题是:我做错了什么?为什么 Hash.cs 中的 foreach 返回 false?
我通过使用 BitConverter.ToString(hashToConvert).Replace("-", string.Empty);
转换 Byte[] 解决了我的问题。
Hash.cs
public void GenerateHashOfString(string stringToHash)
{
hashCode = Encoding.ASCII.GetBytes(stringToHash);
==> hashCodeInStringForm = BitConverter.ToString(hashCode).Replace("-", string.Empty);
linearHashArray.Add(hashCode, stringToHash);
}
public string FindHashCodeInDictionary(string StringToFind)
{
byte[] HashOfStringToFind = Encoding.ASCII.GetBytes(StringToFind);
foreach (var hashCode in linearHashArray)
{
if (!linearHashArray.ContainsKey(HashOfStringToFind) && !linearHashArray.ContainsValue(StringToFind))
{
return "String and hash not found in array";
}
}
return hashCodeInStringForm;
}
我有一个项目,我必须创建自己的线性哈希、桶哈希和二次哈希算法,所以我选择对一些字符串进行哈希处理,但我 运行 遇到了一个小问题2 个列表框:
- 一个充满字符串的列表框
- 一个充满这些字符串的散列的列表框
我可以毫无问题地打印字符串,但是我不知道如何在单击按钮时将它们相应的散列打印到另一个 ListBox 中。
这是我的代码:
Form.cs
private void btnLinearHash_Click(object sender, EventArgs e)
{
// Initialize Hash.cs (see below for code)
Hash.LinearHash linearHash = new Hash.LinearHash();
RandomArrayGen randGen = new RandomArrayGen();
// Clear both ListBoxes
lbHashStrings.Items.Clear();
lbHashesOfStrings.Items.Clear();
for (int i = 0; i < 12; i++)
{
// Generate 12 random upper and lowercase strings using RandomArrayGen.cs (see https://pastebin.com/jRsG5r1E)
lbHashStrings.Items.Add(randGen.GenRandomStrings(12, true, true));
}
for (int i = 0; i < lbHashStrings.Items.Count; i++)
{
// Generate a hash of a string and store it into a Dictionary (see Hash.cs)
linearHash.GenerateHashOfString(lbHashStrings.Items[i].ToString());
lbHashesOfStrings.Items.Add(linearHash.FindHashCodeInDictionary(lbHashStrings.Items[i].ToString()));
}
}
Hash.cs
public class LinearHash
{
private Dictionary<byte[], string> linearHashArray = new Dictionary<byte[], string>();
private byte[] hashCode;
public void GenerateHashOfString(string stringToHash)
{
hashCode = Encoding.ASCII.GetBytes(stringToHash);
linearHashArray.Add(hashCode, stringToHash);
}
public string FindHashCodeInDictionary(string StringToFind)
{
byte[] HashOfStringToFind = Encoding.ASCII.GetBytes(StringToFind);
//For some reason, this keeps returning false
foreach (var keyPairValue in linearHashArray)
{
if ((linearHashArray.ContainsKey(HashOfStringToFind) && linearHashArray.ContainsValue(StringToFind))
{
return Encoding.UTF8.GetString(HashOfStringToFind);
}
}
return "Hash not found";
}
}
所以,我的问题是:我做错了什么?为什么 Hash.cs 中的 foreach 返回 false?
我通过使用 BitConverter.ToString(hashToConvert).Replace("-", string.Empty);
转换 Byte[] 解决了我的问题。
Hash.cs
public void GenerateHashOfString(string stringToHash)
{
hashCode = Encoding.ASCII.GetBytes(stringToHash);
==> hashCodeInStringForm = BitConverter.ToString(hashCode).Replace("-", string.Empty);
linearHashArray.Add(hashCode, stringToHash);
}
public string FindHashCodeInDictionary(string StringToFind)
{
byte[] HashOfStringToFind = Encoding.ASCII.GetBytes(StringToFind);
foreach (var hashCode in linearHashArray)
{
if (!linearHashArray.ContainsKey(HashOfStringToFind) && !linearHashArray.ContainsValue(StringToFind))
{
return "String and hash not found in array";
}
}
return hashCodeInStringForm;
}