echo unsigned 64 bit变量

时间:2017-11-07 14:30:15

标签: linux bash shell hex xor

我有以下shell代码:

Val1=0123456789abcdef
Val2=fedcba9876543210

Val3_dec=$((16#$Val1 ^ 16#$Val2))
Val3=`echo "obase=16; $Val3_dec" | bc`
echo $Val1
echo $Val2
echo $Val3_dec 
echo $Val3

带有相应的输出:

0123456789abcdef
fedcba9876543210
-1
-1

不幸的是Val3的输出是负的,但我需要它是HEX中的64位无符号输出。 像:

0123456789abcdef
fedcba9876543210
-1    (the output of Val3_dec does not matter)
ffffffffffffffff

一些想法?

1 个答案:

答案 0 :(得分:1)

使用printf:

#!/bin/bash
Val1=0123456789abcdef
Val2=fedcba9876543210

Val3_dec=$((16#$Val1 ^ 16#$Val2))
Val3=`echo "obase=16; $Val3_dec" | bc`
echo $Val1
echo $Val2
printf "%d\n" $Val3_dec 
printf "%x\n" $Val3

结果:

$ ./test.sh 
0123456789abcdef
fedcba9876543210
-1
ffffffffffffffff