Search code examples
linuxbashshellhexxor

echo unsigned 64 bit variable


I have the following shell code:

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

with the corresponding output:

0123456789abcdef
fedcba9876543210
-1
-1

unfortunately the output of Val3 is signed negative, but I need it to be a 64 bit unsigned output in HEX. Like:

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

Some ideas?


Solution

  • Use 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
    

    Results:

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