简体   繁体   中英

Increment Hex Values in bash Script

How do I increment a hex value by one and save it to a variable in a loop?

I have echo 'obase=16;ibase=16;A+1' | bc echo 'obase=16;ibase=16;A+1' | bc This will output the correct hex value, but I can't seem to do the calculation with variable expansion. I want to do something like key=$(echo 'obase=16;ibase=16;$key+1' | bc) , but this fails. I need to be able to increment from 0x0 to 0xffff and use the hex value, not just output it. Thanks!

Try something like:

declare -i key

for (( key=0; key<=16#ff; key++ )); do
    printf "%d (dec) = %x (hex), " $key $key
    printf "%x (hex) + 1 = %x (hex)\n" $key $(( $key + 1))
done

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM