【練習】整樹轉換爲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:\n");
    int a ;
    scanf("%d",&a);
    xtoa(a,buf);
    printf("%d --> %s\n",a,buf);
    return 0;
}

下面爲代碼運行結果:測試

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

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

相關文章
相關標籤/搜索