linuxbashunixhexprintf

how i can print or sum one larger hex in bash


when i use

printf "%064X\n" $((0x1a838b13505b26867))

the number one was truncated

000000000000000000000000000000000000000000000000A838B13505B26867

with big hex/number is worse

printf "%064X\n" $((0x349b84b6431a6c4ef1))
0000000000000000000000000000000000000000000000009B84B6431A6C4EF1

number has to be print accurate because i will sum them don't have to be printf can be bc or other thing as long as i can put in bash script is fine

try

printf "%X\n" $((0x349b84b6431a6c4ef1 + 1))
9B84B6431A6C4EF2

expecting

printf "%X\n" $((0x349b84b6431a6c4ef1 + 1))
349B84B6431A6C4EF2

Solution

  • Using gnu awk you can handle this:

    awk -M -v n='0x349b84b6431a6c4ef1' 'BEGIN {printf "%x\n", (strtonum(n) + 1)}'
    
    349b84b6431a6c4ef2