16进制的转换

时间:2020-12-13 11:07:45
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

//using namespace std;

char *get16String(long num)
{
    int i=0;
    char* buffer=(char*)malloc(11);
    char* temp;
    
    buffer[0]='0';
    buffer[1]='x';
    buffer[10]='\0';
    temp=buffer+2;
    
    for(i=0;i<8;i++)
    {
        temp[i]=(char)(num<<4*i>>28);
        printf("### %d ###\t",temp[i]);
        temp[i]=temp[i]>=0?temp[i]:temp[i]+16;
        printf("### %d ###\t",temp[i]);
        temp[i]=temp[i]<10?temp[i]+48:temp[i]+55;
        printf("### %d ###\n",temp[i]);
    }
    return buffer;
}


int main()
{
    char *p=NULL;
    int num=0;
    printf("input num:");
    scanf("%d",&num);
    
    p=get16String(num);
    printf("%s\n",p);
    
    return 0;
}