- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试查找任何给定文件的熵。然而,当我运行我的程序时,它总是给出 3.00000 作为答案。我已经有一段时间没有使用 C 了,但我不确定我哪里出错了。我已经摆弄它几个小时了。任何提示都会很棒,谢谢!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define SIZE 256
int entropy_calc(long byte_count[], int length)
{
float entropy;
float count;
int i;
/* entropy calculation */
for (i = 0; i < SIZE; i++)
{
if (byte_count[i] != 0)
{
count = (float) byte_count[i] / (float) length;
entropy += -count * log2f(count);
}
}
return entropy;
}
int main(int argc, char **argv)
{
FILE *inFile;
int i;
int j;
int n; // Bytes read by fread;
int length; // length of file
float count;
float entropy;
long byte_count[SIZE];
unsigned char buffer[1024];
/* do this for all files */
for(j = 1; j < argc; j++)
{
memset(byte_count, 0, sizeof(long) * SIZE);
inFile = fopen(argv[j], "rb"); // opens the file given on command line
if(inFile == NULL) // error-checking to see if file exists
{
printf("Files does not exist. `%s`\n", argv[j]);
continue;
}
/* Read the whole file in parts of 1024 */
while((n = fread(buffer, 1, 1024, inFile)) != 0)
{
/* Add the buffer to the byte_count */
for (i = 0; i < n; i++)
{
byte_count[(int) buffer[i]]++;
length++;
}
}
fclose(inFile);
float entropy = entropy_calc(byte_count, length);
printf("%02.5f \t%s\n", entropy, argv[j]);
}
return 0;
}
最佳答案
entropy_calc()
函数的返回类型应该是 float 而不是 int。
关于在 C 中计算熵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25985005/
我们有一个 WPF 应用程序,允许用户下载加密内容,并且我们希望提供离线解密该内容的功能。我的想法是下载 key 并使用 DPAPI 存储它们,但我在使用熵参数时遇到了问题。 是否有任何方法可以生成熵
在 php.ini 的 session 部分,有一个名为 session.entropy_length 的指令。 我知道它用于使 session ID 的生成“更加随机”。 它如何使 session
在 php.ini 的 session 部分,有一个名为 session.entropy_length 的指令。 我知道它用于使 session ID 的生成“更加随机”。 它如何使 session
我使用决策树算法来解决二元分类问题,目标是最大限度地减少分类的误报(最大化阳性预测值)(诊断工具的成本非常高) 。 有没有办法在基尼/熵分割标准中引入权重来惩罚误报错误分类? Here例如,修改后的基
我想检查我的 std::random_device 实现是否具有非零熵(即非确定性),使用 std::random_device::entropy() 函数。然而,根据至cppreference.co
我在 tensorflow_decision_forests 文档 ( https://github.com/tensorflow/decision-forests ) ( https://www.t
我是一名优秀的程序员,十分优秀!