简体   繁体   English

不正确的十进制到十六进制转换-C

[英]Incorrect decimal to hexadecimal conversion - C

I wrote this function from a pseudocode I found, which should convert decimal input into hexadecimal number. 我从找到的伪代码编写了此函数,该伪代码应将十进制输入转换为十六进制数字。 Well it does that, but in incorrect order, like for example, for decimal number 195 I get 3C, insted of C3. 可以做到这一点,但是顺序不正确,例如,对于十进制数字195,我得到3C,由C3插入。

int temp=0;
int i=0;
while(decimal!=0)
{
    temp = decimal % 16;
    if( temp < 10)
        temp =temp + 48;
    else
        temp = temp + 55;
    array[i++]= temp;
    decimal = decimal / 16;
}

save yourself some time like this 这样可以节省一些时间

#include <stdio.h>
// ...
sprintf(hexStr,"%X",decimal);   // or, "%#X" if you want prefix

unless, this is homework for a programming class. 除非这是编程课程的功课。 in which case you should really just work it out on whiteboard or paper, i'm sure you'll see your mistake. 在这种情况下,您实际上应该只在白板或纸上进行处理,我确定您会看到自己的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM