简体   繁体   中英

How to increment variable value from 0x00 to 0x11 (in hex)

Can anyone tell how to increment variable value by one from 0x00 to 0x04 in the bash. I want to generate:

0x00
0x01
0x02
0x03
0x04

But I am getting the following result:

0
1
2
3
4

My code is :

#!/bin/bash
for (( num=0x0; num <= 0x04 ; num+=1 ));
do 
    echo $num
done

Use printf to print the number as hex:

for (( num=0x0; num <= 0x20 ; num+=1 )); do 
    printf '0x%02X\n' $num
done

Note that num is nothing special. Even though you initialize it with 0x0 , it's just 0. Even when we pass num to printf , it is done so as decimal (so 0x11 is passed as 17). It is printf that formats your number as hex.

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