我使用此代码计算十六进制值的熵
public static double ShannonEntropy(string s)
{
string[] hexValuesSplit = s.Split(' ');
var map = new Dictionary<int, int>();
foreach (String hex in hexValuesSplit)
{
int value = Convert.ToInt32(hex, 16);
if (!map.ContainsKey(value))
map.Add(value, 1);
else
map[value] += 1;
}
double result = 0.0;
int len = s.Length;
foreach (var item in map)
{
var frequency = (double)item.Value / len;
result -= frequency * (Math.Log(frequency) / Math.Log(2));
}
return result;
}
这样的十六进制值&#34; 4D 5A 68 6B 6A 68 6A 00 02 00 00 00 FF FF&#34; 我检查了s.Length返回不正确的大小! 为什么结果熵不正确?