我有一个 1991 的整数输入。如何将输入转换为 0x1991 而不是 7C7?我已经编写了将十六进制转换为十进制的代码(答案将是 6545)
int ui = 0x202;
int BCD(int value) //Converting 0xhex to decimal
{
int result = 0;
result <<= 4;
result |= ui % 10;
ui /= 10;
return value;
}
最佳答案
const int input = 1991;
int i = input;
int f = 1;
int result = 0x0;
while( i > 0 )
{
int d = i % 10;
result += d * f;
f *= 16;
i /= 10;
}
printf("0x%x", result ); // 0x1991
关于将整数转换为 0x 等价物(十六进制),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40774310/