如何在C中递增/递减十六进制值

时间:2014-11-29 14:29:26

标签: c hex increment

我试图使用十六进制创建一个计数器,我知道这可以很容易地使用十进制来完成,但是可以用十六进制来完成,或者它在十进制中是否相同?

会这样吗?

myhex = 0x00;

myhex++;

还是会以完全不同的方式完成?

如果你问为什么使用hex,因为这是针对MCU的,对我而言,控制MCU IO的最佳方法是使用hex。

1 个答案:

答案 0 :(得分:9)

是的,如果您尝试它,您会看到它,如果numberhexoctaldecimal,则没有任何区别!

举个例子:

#include <stdio.h>

int main() {

    int myhex = 0x07;
    int myOct = 07;
    int myDec = 7;

    printf("Before increment:\n");
    printf("Hex: %x\n", myhex);
    printf("Oct: %o\n", myOct);
    printf("Dec: %d\n", myDec);

    myhex++;
    myOct++;
    myDec++;

    printf("After increment:\n");
    printf("Hex: %x\n", myhex);
    printf("Oct: %o\n", myOct);
    printf("Dec: %d\n", myDec);


  return 0;

}

输出:

Before increment:
Hex: 7
Oct: 7
Dec: 7

After increment:
Hex: 8
Oct: 10
Dec: 8
相关问题