gpt4 book ai didi

python - 哪些信息描述了两个相同大小的给定大文件之间的数量差异?

转载 作者:太空狗 更新时间:2023-10-29 12:37:17 26 4
gpt4 key购买 nike

通常,为了找出两个二进制文件有何不同,我使用 diff 和 hexdump 工具。但在某些情况下,如果给出两个相同大小的大型二进制文件,我只想看到它们的数量差异,如差异区域数、累积差异。

示例:2 个文件 A 和 B。它们有 2 个差异区域,它们的累积差异为6c-a3 + 6c-11 + 6f-6e + 20-22.

File A = 48 65 6c 6c 6f 2c 20 57
File B = 48 65 a3 11 6e 2c 22 57
|--------| |--|
reg 1 reg 2

我如何使用标准 GNU 工具和 Bash 获取此类信息,或者我应该更好地使用简单的 Python 脚本?关于 2 个文件有何不同的其他统计数据也很有用,但我不知道还有什么以及如何衡量?熵差?方差差异?

最佳答案

对于除区域之外的所有内容,您都可以使用 numpy .像这样的东西(未经测试):

import numpy as np
a = np.fromfile("file A", dtype="uint8")
b = np.fromfile("file B", dtype="uint8")

# Compute the number of bytes that are different
different_bytes = np.sum(a != b)

# Compute the sum of the differences
difference = np.sum(a - b)

# Compute the sum of the absolute value of the differences
absolute_difference = np.sum(np.abs(a - b))

# In some cases, the number of bits that have changed is a better
# measurement of change. To compute it we make a lookup array where
# bitcount_lookup[byte] == number_of_1_bits_in_byte (so
# bitcount_lookup[0:16] == [0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4])
bitcount_lookup = np.array(
[bin(i).count("1") for i in range(256)], dtype="uint8")

# Numpy allows using an array as an index. ^ computes the XOR of
# each pair of bytes. The result is a byte with a 1 bit where the
# bits of the input differed, and a 0 bit otherwise.
bit_diff_count = np.sum(bitcount_lookup[a ^ b])

我找不到用于计算区域的 numpy 函数,但只需使用 a != b 作为输入编写您自己的函数,应该不难。参见 this寻找灵感的问题。

关于python - 哪些信息描述了两个相同大小的给定大文件之间的数量差异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7834123/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com