Cor*_*ein 7 software-rec ascii files
给定一个myfile包含以下内容的文件:
$ cat myfile
foos
Run Code Online (Sandbox Code Playgroud)
文件的十六进制转储为我们提供了内容:
$ hexdump myfile
6f66 736f 000a
Run Code Online (Sandbox Code Playgroud)
目前,我可以通过在 ascii 中指定内容来创建文件,如下所示:
$ echo foos > myfile
Run Code Online (Sandbox Code Playgroud)
是否可以通过以十六进制而不是 ascii 提供确切字节来创建文件?
$ # How can I make this work?
$ echo --fake-hex-option "6f66 736f 000a" > myfile
$ cat myfile
foos
Run Code Online (Sandbox Code Playgroud)
更新:为了清楚起见,我提出了这个问题,询问如何将少量字节写入文件。实际上,我需要一种方法将大量十六进制数直接通过管道传输到文件中,而不仅仅是 3 个字节:
$ cat hexfile
6f66 736f 6f66 736f ...
$ some_utility hexfile > myfile
$ cat myfile
foosfoosfoosfoos...
Run Code Online (Sandbox Code Playgroud)
您可以使用echo -e:
echo -e "\x66\x6f\x6f"
Run Code Online (Sandbox Code Playgroud)
请注意,这hexdump -C是您要按字节顺序转储文件内容,而不是按网络字节顺序解释为 4 字节字。
这是hexundump我个人收藏的脚本:
#!/usr/bin/env perl
$^W = 1;
$c = undef;
while (<>) {
tr/0-9A-Fa-f//cd;
if (defined $c) { warn "Consuming $c"; $_ = $c . $_; $c = undef; }
if (length($_) & 1) { s/(.)$//; $c = $1; }
print pack "H*", $_;
}
if (!eof) { die "$!"; }
if (defined $c) { warn "Odd number of hexadecimal digits"; }
Run Code Online (Sandbox Code Playgroud)