十进制到十六进制之间的转换

时间:2012-09-17 16:45:23

标签: c++ hex

unsigned int hex_vale, some_decimal_constant, some_other_decimal_constant;

for(10,000 times){
if(hex_value == some_decimal_constant){
        call_some_function();
    }
else if(hex_value == some_other_decimal_constant){
        call_some_other_function();     
    }
}

在上面执行的函数中,如果循环I必须将十六进制转换为十进制,反之亦然。我不想将十六进制转换为十进制10000次。能否帮助我将十进制转换为十六进制,十六进制数应存储在无符号整数中。

1 个答案:

答案 0 :(得分:4)

您无需执行任何转化。数字以二进制形式存储。在这种情况下,十六进制和十进制只是这些二进制数的不同文字或字符表示。

示例:

#include <iostream>

int main()
{
  std::cout << std::boolalpha;
  const int a = 32;
  const int b = 0x20;
  std::cout << (a==b) << "\n"; // prints "true"

}