简体   繁体   English

如何将十六进制编码的字符串转换为Perl中的字节字符串?

[英]How to convert a hex-encoded string to a byte string in Perl?

My original code is in Python , but I need to convert it to Perl for some libraries that I don't have at my disposal in Python. 我的原始代码是用Python编写的,但是我需要将它转换为Perl,这些库是我在Python中没有的。

In Python I would do this: 在Python中我会这样做:

packet=binascii.unhexlify('F0000000F6905C452001A8C0000000000160994E810FB54E0100DB0000000000000')

AND

This would create a string containing the binary representation of: 这将创建一个包含二进制表示形式的字符串:

0xF0 0x00 0x00 0x00 0xF6 0x90 0x5C 0x45 etc...

Now that my string is a byte array I can send it as the payload for my packet. 现在我的字符串是一个字节数组,我可以将它作为我的数据包的有效负载发送。 How do I do it Perl? 我怎么做Perl?

You can use the pack function for this. 您可以使用pack功能。

Example: 例:

$ perl -e 'print pack("H*", "303132616263"), "\n";'
012abc

Check out the pack tutorial . 查看包教程

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

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