Perl 包功能坏了?
我有一个标头创建 Perl 脚本,它在大多数情况下都工作得很好,但每隔一段时间就会出现问题。鉴于 CRC 编号 772423333,PERL pack 函数中断,我将直接进入其核心。
my $dec = 772423333;
my $broken = pack("N", $dec);
print "Good:\t", uc(sprintf("%x", $dec)), "\nBad:\t$broken"; # eg. 2E0D0A3EA5
请原谅我不知道如何打印可读的十六进制,但这就是它返回的内容。
Good: 2E0A3EA5
Bad: 2E0D0A3EA5
如何删除0D
?
I have a header creation Perl script and it works great most of the time, but every once in a while the thing breaks. I'll get right down to the meat of it, given the CRC number 772423333 the PERL pack function breaks.
my $dec = 772423333;
my $broken = pack("N", $dec);
print "Good:\t", uc(sprintf("%x", $dec)), "\nBad:\t$broken"; # eg. 2E0D0A3EA5
Forgive me for not knowing how to print the readable HEX, but this is what it returns.
Good: 2E0A3EA5
Bad: 2E0D0A3EA5
How do I remove the 0D
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的示例输出不是您的程序打印的内容。您的程序以二进制形式打印出“坏”的内容(就好像它是可打印字符,尽管事实并非如此),而不是以十六进制形式。
它在这里工作(一旦我将它通过管道传输到十六进制转储器,这样我就可以读取它),但我在 Linux 上。
最有可能的是,你出错的地方是你需要在输出文件句柄上调用
binmode
(或者使用:raw
层打开它);您正在看到 CRLF 翻译的换行符。如果您在打印之前添加binmode *STDOUT;
(在示例代码中),我怀疑您会得到预期的输出。[ 在 Unix 上,没有换行符到 CRLF 的转换,所以它可以工作 ]
Your example output isn't what your program prints. Your program prints "bad" out that in binary (as if it were printable characters, though its not), not in hex.
It works here (once I pipe it to a hex dumper, so I can read it), but I'm on Linux.
Most likely, where you're going wrong is that you need to call
binmode
on your output file handle (or alternatively open it with a:raw
layer); you are seeing newline to CRLF translation. If you addbinmode *STDOUT;
immediately before your print (in your example code), I suspect you'll get the expected output.[ On Unix, there is no newline-to-CRLF translation, so it works ]
停止使用 Windows?
0D0A
是 Windows 行结尾的字符代码(更常见的是"\r\n"
),您观察它们是因为您正在打印字符0A
("\n"
) 到具有:crlf
编码的句柄 (STDOUT
),它会自动转换任何\n
个字符添加到序列中<代码>\r\n。在
STDOUT
上调用binmode
以禁用此编码。下面是使用 MSWin32 构建的 perl 和 Cygwin 实用程序 od 的视图:Stop using Windows?
0D0A
are the character codes of a Windows line ending (more commonly seen as"\r\n"
), and you observe them because you are printing character0A
("\n"
) to a handle (STDOUT
) with the:crlf
encoding, which automatically converts any\n
characters to the sequence\r\n
.Call
binmode
onSTDOUT
to disable this encoding. Here's the view using an MSWin32 build of perl with the Cygwin utilityod
: