gpt4 book ai didi

perl - 在 Perl 中解析二进制文件并返回指定偏移量的值

转载 作者:行者123 更新时间:2023-12-02 08:42:32 26 4
gpt4 key购买 nike

我有一个需要能够解析的二进制文件。我希望指定一个偏移量,然后让程序返回该位置的字节值。

我不确定该怎么做。我有文件打开部分,但是我不知道如何让程序跳转到该位置。

最佳答案

use Fcntl qw(:seek);

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

$filename = "/some/file/name/goes/here";
$byte_position = 42;

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;

关于perl - 在 Perl 中解析二进制文件并返回指定偏移量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15298974/

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