简体   繁体   中英

Convert raw hex to readable hex in perl?

I have written a little perl app to reads the serial port. When I run my little script I receive data but it's written in unreadable signs.. it shows like *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? Right now I use print ($data) to print my 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 . For example,

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

Use H instead of h if you want the opposite endianness. (I always forget which is which.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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