- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我试图从谷歌、论坛、维基百科和许多论坛中搜索此功能两个多小时,但我找不到它。我该怎么做?我尝试了以下但没有用。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <stdint.h>
static unsigned int mylog2 (unsigned int val) {
unsigned int ret = -1;
while (val != 0) {
val >>= 1;
ret++;
}
return ret;
}
int main(int argc, char **argv)
{
FILE *pFile;
int i; // various loop index
int j; // filename loop index
int n; // Bytes read by fread;
int size; // Filesize
float entropy;
float temp; // temp value used in entropy calculation
long alphabet[256];
unsigned char buffer[1024];
/* do this for all files */
for(j = 1; j < argc; j++)
{
/* initialize all values */
size = 0;
entropy = 0.0;
memset(alphabet, 0, sizeof(long) * 256);
pFile = fopen(argv[j], "rb");
if(pFile == NULL)
{
printf("Failed to open `%s`\n", argv[j]);
continue;
}
/* Read the whole file in parts of 1024 */
while((n = fread(buffer, 1, 1024, pFile)) != 0)
{
/* Add the buffer to the alphabet */
for (i = 0; i < n; i++)
{
alphabet[(int) buffer[i]]++;
size++;
}
}
fclose(pFile);
/* entropy calculation */
for (i = 0; i < 256; i++)
{
if (alphabet[i] != 0)
{
temp = (float) alphabet[i] / (float) size;
entropy += -temp * mylog2(temp);
}
}
printf("%02.5f [ %02.5f ]\t%s\n", entropy, entropy / 8, argv[j]);
} // outer for
return 0;
}
我知道我做错了。在 python 中它似乎容易得多,在 python 中它是:
import sys
import math
if len(sys.argv) != 2:
print "Usage: file_entropy.py [path]filename"
sys.exit()
# read the whole file into a byte array
f = open(sys.argv[1], "rb")
byteArr = map(ord, f.read())
f.close()
fileSize = len(byteArr)
print 'File size in bytes:'
print fileSize
print
# calculate the frequency of each byte value in the file
freqList = []
for b in range(256):
ctr = 0
for byte in byteArr:
if byte == b:
ctr += 1
freqList.append(float(ctr) / fileSize)
# print 'Frequencies of each byte-character:'
# print freqList
# print
# Shannon entropy
ent = 0.0
for freq in freqList:
if freq > 0:
ent = ent + freq * math.log(freq, 2)
ent = -ent
print 'Shannon entropy (min bits per byte-character):'
print ent
print
print 'Min possible file size assuming max theoretical compression efficiency:'
print (ent * fileSize), 'in bits'
print (ent * fileSize) / 8, 'in bytes'
### Modifications to file_entropy.py to create the Histogram start here ###
### by Ken Hartman www.KennethGHartman.com
import numpy as np
import matplotlib.pyplot as plt
N = len(freqList)
ind = np.arange(N) # the x locations for the groups
width = 1.00 # the width of the bars
#fig = plt.figure()
fig = plt.figure(figsize=(11,5),dpi=100)
ax = fig.add_subplot(111)
rects1 = ax.bar(ind, freqList, width)
ax.set_autoscalex_on(False)
ax.set_xlim([0,255])
ax.set_ylabel('Frequency')
ax.set_xlabel('Byte')
ax.set_title('Frequency of Bytes 0 to 255\nFILENAME: ' + sys.argv[1])
plt.show()
如何在 C++ 中实现相同的功能?希望有人如实回答。
最佳答案
您不能计算以 2 为底的对数的整数部分。要在 C 中以 2 为底计算对数,您可以使用 math.h
中的 log2
。
关于c++ - 计算文件的熵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18962990/
我们有一个 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
我是一名优秀的程序员,十分优秀!