十进制转十六进制

代码:

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    int n;
    int count=0;
    int temp;
    int i;
    char num[10000];
    scanf("%d",&n);
    do{
        temp = n%16;
        if(temp>9){
            switch(temp){
                case 10: num[count]='A'; break;
                case 11: num[count]='B'; break;
                case 12: num[count]='C'; break;
                case 13: num[count]='D'; break;
                case 14: num[count]='E'; break;
                case 15: num[count]='F'; break;
            }
            count++;
            
        }else{
            num[count]=n%16+'0';
            count++;
        }
        n = n/16;
        
        
    }while(n!=0);
    
    for(i=count-1;i>=0;i--){
        printf("%c",num[i]);
    }
    return 0;
}

运行结果:

 

posted @ 2021-04-17 20:35  ZengZG  Views(431)  Comments(0Edit  收藏  举报