如何在Linux Shell中将十六进制转换为ASCII字符? [英] How to convert hex to ASCII characters in the Linux shell?

查看:1379
本文介绍了如何在Linux Shell中将十六进制转换为ASCII字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有一个字符串5a.

Lets say that I have a string 5a.

这是ASCII字母Z的十六进制表示.

This is the hex representation of the ASCII letter Z.

我需要知道一个Linux shell命令,该命令将接受一个十六进制字符串并输出该字符串表示的ASCII字符.

I need to know a Linux shell command which will take a hex string and output the ASCII characters that the string represents.

所以,如果我这样做:

echo 5a | command_im_looking_for

我会看到一个单独的字母Z:

I will see a solitary letter Z:

Z

推荐答案

echo -n 5a | perl -pe 's/([0-9a-f]{2})/chr hex $1/gie'

请注意,这不会跳过非十六进制字符.如果只需要十六进制(原始字符串中没有空格等):

Note that this won't skip non-hex characters. If you want just the hex (no whitespace from the original string etc):

echo 5a | perl -ne 's/([0-9a-f]{2})/print chr hex $1/gie'

此外,zshbashecho中本身支持此功能:

Also, zsh and bash support this natively in echo:

echo -e '\x5a'

这篇关于如何在Linux Shell中将十六进制转换为ASCII字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆