英文:
convert an array of hexadecimal octets to a string variable in bash
问题
以下是您要翻译的内容:
"Is there a "better" way to convert an array of hexadecimal to a string variable in shell other than the following?
Maybe via variable substitution?
# the following is a hex representation of the string "filename.txt"
a=(66 69 6c 65 6e 61 6d 65 2e 74 78 74);
var="";
for i in ${a[@]}; do \
var+=$(echo -n -e "\x$i");
done;
echo $var;
# -> filename.txt
I'm searching for a plain bash solution without using perl, python nor other script languages."
您希望的翻译如下:
"有没有一种比以下方法更好的方式在Shell中将十六进制数组转换为字符串变量?
也许可以通过变量替换来实现吗?
# 以下是字符串 "filename.txt" 的十六进制表示
a=(66 69 6c 65 6e 61 6d 65 2e 74 78 74);
var="";
for i in ${a[@]}; do \
var+=$(echo -n -e "\x$i");
done;
echo $var;
# -> filename.txt
我正在寻找一种纯Bash解决方案,不使用Perl、Python或其他脚本语言。"
英文:
Is there a "better" way to convert an array of hexadecimal to a string variable in shell other than the following?
Maybe via variable substitution?
# the following is a hex representation of the string "filename.txt"
a=(66 69 6c 65 6e 61 6d 65 2e 74 78 74);
var="";
for i in ${a[@]}; do \
var+=$(echo -n -e "\x$i");
done;
echo $var;
# -> filename.txt
I'm searching for a plain bash solution without using perl, python nor other script languages.
答案1
得分: 5
以下是翻译好的部分:
这是一种方法:
$ a=(66 69 6c 65 6e 61 6d 65 2e 74 78 74)
$ printf -v var '%b' "${a[@]/#/\\x}"
$ echo "$var"
filename.txt
有关更多信息,请查看 printf
和 Shell Parameter Expansion。
英文:
Here is one way:
$ a=(66 69 6c 65 6e 61 6d 65 2e 74 78 74)
$ printf -v var '%b' "${a[@]/#/\\x}"
$ echo "$var"
filename.txt
$
See printf
and Shell Parameter Expansion for further information.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论