perl - 如何在 Perl 中创建二进制文件?

标签 perl

od -x test 显示:

0000000 457f 464c 0102 0001

现在我想使用 Perl 来创建这样的文件。

open FH,'>','test_1';
#syswrite(FH,0x457f464c01020001); # not work
print FH 0x457f464c01020001;      # not work

如何在 Perl 中创建二进制文件?

最佳答案

要创建二进制文件,可以使用

open (my $fh, '>:raw', $qfn)

放置

45 7f 46 4c 01 02 00 01

在该文件中,可以使用以下任何一种:

# Starting with a string of those bytes.
print $fh "\x45\x7f\x46\x4c\x01\x02\x00\x01";

# Starting with a hex representation of the file.
print $fh pack('H*', '457f464c01020001');

# Starting with the bytes.
print $fh map chr, 0x45, 0x7f, 0x46, 0x4c, 0x01, 0x02, 0x00, 0x01;

# Starting with the bytes.
print $fh pack('C*', 0x45, 0x7f, 0x46, 0x4c, 0x01, 0x02, 0x00, 0x01);

关于perl - 如何在 Perl 中创建二进制文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9494383/

相关文章:

javascript - 在 njs (javascript) 中用连字符拆分驼峰式单词的正则表达式

perl - 何时使用 $sth->fetchrow_hashref、$sth->fetchrow_arrayref 和 $sth->fetchrow_array?

Perl dbd-sqlite,是否有等同于 .import 函数的函数?

php - 排序/解析 MySQL 行字段中的异构 CSV 分隔文本值

perl - 根据标题和日期计算行数

perl - 当 Perl 中的命令执行失败时,如何读取 stderr?

perl - 远程调试Perl,Dancer,Starman,Docker

perl - 无法修改标量赋值中的常量项

linux - 如何将变量作为键传递给perl中的散列

arrays - 定义新数据结构时使用引用连接数组