简体   繁体   English

将原始十六进制转换为Perl中的可读十六进制?

[英]Convert raw hex to readable hex in perl?

I have written a little perl app to reads the serial port. 我已经编写了一个小Perl应用程序来读取串行端口。 When I run my little script I receive data but it's written in unreadable signs.. it shows like *I??. 当我运行我的小脚本时,我收到了数据,但是它是以不可读的符号写的。它显示为* I ??。 However if I do 但是如果我这样做

perl test.pl | hexdump

I get the required data. 我得到了所需的数据。 And the hex data makes sense to me. 十六进制数据对我来说很有意义。 Does anyone know how I can get this output using perl without using hexdump? 有谁知道我如何在不使用hexdump的情况下使用perl获得此输出? Right now I use print ($data) to print my data. 现在,我使用print($ data)打印我的数据。

"Raw hex" doesn't mean anything; “原始十六进制”不代表任何含义; what you've got is a string of bytes that you want to convert to a textual representation. 您得到的是一串要转换为文本表示形式的字节。 To do that you can use unpack . 为此,您可以使用unpack For example, 例如,

my $bytes = read_from_serial_port();
my $hex = unpack 'h*', $bytes;

Use H instead of h if you want the opposite endianness. 如果想要相反的字节序,请使用H而不是h (I always forget which is which.) (我总是忘记哪个是哪个。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM