使用perl -e将十六进制转换为ASCII

huangapple go评论46阅读模式
英文:

Converting hex to ASCII with perl -e

问题

perl -e "print chr(hex('41'));"
英文:

I want to print the ASCII equivalent of hex in perl -e in one line.

perl -e "print 0x41;"

I'm getting 65, but I want the output to be A

How can I convert from hex to ASCII in just one line command.

答案1

得分: 7

$ perl -e 'printf "%c\n", 0x41'
A
$ perl -E 'say pack("c", 0x41)'
A
$ perl -E 'say chr(0x41)'
A
英文:

A few ways:

$ perl -e 'printf "%c\n", 0x41'
A
$ perl -E 'say pack("c", 0x41)'
A
$ perl -E 'say chr(0x41)'
A

答案2

得分: 0

要将十六进制字符串转换为ASCII,您可以使用Perl的pack函数:

$ perl -le 'print pack("H*", "0x41");'

其中:
-l:启用自动处理换行符
-e:在命令行直接指定Perl代码
H*:pack函数的格式说明符

输出:

A
英文:

To convert a hex string to ASCII, you can use Perl's pack function:

$ perl -le 'print pack("H*", "0x41");'

Where:
-l: enables automatic line-ending processing
-e: to specify the Perl code directly on the command line
H*: format specifier with pack function

Output:

A

huangapple
  • 本文由 发表于 2023年7月17日 14:28:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701965.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定