gpt4 book ai didi

windows - 如何通过脚本在windows下编辑二进制文件

转载 作者:可可西里 更新时间:2023-11-01 11:50:37 26 4
gpt4 key购买 nike

我通常是一个读者,但这次我找不到答案。我有一些由科学设备生成的技术文件。有时,录制的文件会损坏,我们必须手动进行一些十六进制修改。我想知道如何使它自动化。我在考虑 Perl,因为我在这方面有一些知识,但即使我设法读取了感兴趣的偏移量,我也不知道如何编写新值。

我有两件事要做:

  1. 在偏移量 4 处写入文件大小减 8
  2. 计算“TRCKfmt”模式的个数,十六进制为 5452434B666D74,然后在偏移量 5C(92)处记下十六进制值。

我尝试在文件句柄上使用 sysreadsyswrite,但我无法完成不同的步骤。

也许 Perl 不是一个好的选择,我不知道如何解决。

这是我的实际脚本:

use warnings;
use strict;
use diagnostics;

use Fcntl qw(:seek);

my($fh, $filename, $byte_position, $byte_value);

$filename = "MYFILE.tac";
$byte_position = 4;
my $filesize = -s $filename;
print "Size: $filesize\n";


open($fh, "<", $filename)
|| die "can't open $filename: $!";

binmode($fh)
|| die "can't binmode $filename";

sysseek($fh, $byte_position, SEEK_CUR) # NB: 0-based
|| die "couldn't see to byte $byte_position in $filename: $!";

sysread($fh, $byte_value, 1) == 1
|| die "couldn't read byte from $filename: $!";

printf "read byte with ordinal value %#02x at position %d\n",
ord($byte_value), $byte_position;

感谢您的帮助。

最佳答案

让自己轻松一点,只需将整个文件加载到内存中即可。

my $qfn = "MYFILE.tac";

my $file;
{
open(my $fh, '<:raw', $qfn)
or die("Can't open \"$qfn\": $!\n");

local $/;
$file = <$fh>;
}

{
my $packed_length = pack('N', length($file) - 8);
substr($file, 0x0004, length($packed_length), $packed_length);
}

{
my $num_blocks;
++$num_blocks while $file =~ /TRCKfmt/g;
my $packed_num_blocks = pack('N', $num_blocks);
substr($file, 0x005C, length($packed_num_blocks), $packed_num_blocks);
}

{
open(my $fh, '>:raw', $qfn)
or die("Can't create \"$qfn\": $!\n");

print($fh $file);
}

你没有说应该以什么格式存储数字。我假设它们是大端字节顺序的 32 位无符号整数。

关于windows - 如何通过脚本在windows下编辑二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30786531/

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