将一个十六进制八位数组转换为Bash中的字符串变量。

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

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

有关更多信息,请查看 printfShell 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.

huangapple
  • 本文由 发表于 2023年2月26日 21:58:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572486.html
匿名

发表评论

匿名网友

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

确定