Perl 将二进制流转换为十六进制
我遇到的问题是,当我有一个 Perl 脚本通过 STDIN 读取数据(PE 可执行文件)并且流包含行终止符“0A”时,到十六进制的转换会丢失它。然后,当我将十六进制数据转换回来时,它已损坏(十六进制格式中缺少 0A)。那么如何在 Perl 中检测换行符“0A”的“windows”版本呢?
注意:Linux 操作系统 (Perl) 正在读取 Windows PE
!usr/bin/perl
while($line = <STDIN>)
{
chomp($line);
@bytes = split //, $line;
foreach (@bytes)
{
printf "%02lx", ord $_;
}
}
用法示例:
[root@mybox test]# cat test.exe | perl encoder.pl > output
The problem I am having is when I have a Perl script reading data (PE Executable) via STDIN and the stream contains a line terminator "0A" the conversion to hex misses it. Then when I convert the hex data back it is corrupted (missing 0A in the hex format). So how can I detect the "windows" version of line feed "0A" in Perl?
Note: Linux OS (Perl) is reading a Windows PE
!usr/bin/perl
while($line = <STDIN>)
{
chomp($line);
@bytes = split //, $line;
foreach (@bytes)
{
printf "%02lx", ord $_;
}
}
Usage example:
[root@mybox test]# cat test.exe | perl encoder.pl > output
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在循环中,您在每个输入行上运行
chomp
。这将从行尾删除当前$/
中的任何值。这很可能是0x0a
,这就是该值的去向。尝试从循环中删除chomp($line)
。一般来说,使用面向行的读取对于本身不是面向行的二进制文件没有意义。您应该看一下较低级别的 read 函数,它允许您从文件中读取字节块,而无需关心这些字节是什么。然后,您可以按块而不是按行处理数据。
In your loop, you are running
chomp
on each input line. This is removing whatever value is currently in$/
from the end of your line. Chances are this is0x0a
, and that's where the value is going. Try removingchomp($line)
from your loop.In general, using line oriented reading doesn't make sense for binary files that are themselves not line oriented. You should take a look at the lower level
read
function which allows you to read a block of bytes from the file without caring what those bytes are. You can then process your data in blocks instead of lines.