【练习】整树转换为16进制的字符串的函数

//完整的测试代码如下:
void xtoa(int a,char*buf){
    int i = 9;
    if(a < 0){
        //return;
        //code later
    }
    do{
        int tmp = a&15;
        if(tmp < 10){
            buf[i] = tmp+'0';
        }
        else{
            buf[i] = tmp+'A'-10;
        }
        if(a < 0)
        {
            a = (a&0X7FFFFFFF)>>4|0X8000000;
            i--;
            continue;
        }
        a = a>>4;
        i--;
    }while(a != 0); 
}
#include<stdio.h>
int main(int argc, const char *argv[])
{
    char *buf;
    char a0[10] = "0x00000000";
    buf = a0;
    printf("please:
");
    int a ;
    scanf("%d",&a);
    xtoa(a,buf);
    printf("%d --> %s
",a,buf);
    return 0;
}

下面为代码运行结果:

root@bevan-VirtualBox:~/code/stupid# ./a.out   
please:
127896
127896 --> 0x0001F398

root@bevan-VirtualBox:~/code/stupid# ./a.out   
please:
-98675
-98675 --> 0xFFFE7E8D

原文地址:https://www.cnblogs.com/wanzaixiaoxin/p/3386794.html