将整数转换为 0x 等价物(十六进制)

标签 c bcd

我有一个 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/

相关文章:

c - 将 char 类型转换为结构体是如何工作的

c++ - 在 C/C++ 中数组是否以相反的顺序存储?

c - 在 c 中读取名称值对的最佳方法

python - 参数指向 PySys_SetArgv() 的字符串是否应该保留在内存中直到 Py_Finalize()?

c# - 在 C# 中使用(复杂的)C 结构

assembly - 添加2个bcd数字-mips

c++ - 将 Unsigned Int 转换为 BCD

digital - 将六位二进制数转换为对应的两位BCD数?

Verilog 多个驱动程序