将32/64位数转换为4/8字符串

时间:2016-04-01 15:32:14

标签: bash

鉴于:

$ printf "love" | od -td4 -A n
  1702260588
$ printf "lovehate" | od -td8 -A n
  7310575196135911276

Bash中是否有简洁(理想情况下没有循环,awk,sed,perl或python)方式将数字17022605887310575196135911276转换为lovelovehate分别?

1 个答案:

答案 0 :(得分:4)

以下是我提出的建议:

alpha() {
  (($1)) && printf "\x"$(printf "%02x" $(($1%256)))$(alpha $(($1/256)))"\n"
}

alpha 1702260588
alpha 7310575196135911276

输出:

love
lovehate

编辑:这是使用xxd实用程序的答案:

# The echo is only necessary to get a newline at the end.
echo $(printf "%x" 1702260588 | xxd -r -p | rev)
echo $(printf "%x" 7310575196135911276 | xxd -r -p | rev)

输出:

love
lovehate