任意进制的转换函数

//

//  main.c

//  baseconv

//

//  Created by Cheney Shen on 11-4-16.

//  Copyright 2011年 __MyCompanyName__. All rights reserved.

//



#include <stdio.h>

#include <limits.h>

#include <string.h>



char* baseconv(unsigned long long, int);



int main (int argc, const char * argv[])

{

    printf("the result is %s", baseconv(3942095803582093809, 2));

    return 0;

}



char* baseconv(unsigned long long num, int base) {

    static char retbuf[sizeof(int)*CHAR_BIT + 1];

    char *p;

    

    if(base < 2 || base > 16)

        return NULL;

    

    p = &retbuf[sizeof(retbuf) - 1];

    *p = '\0';

    

    do{

        *--p = "0123456789abcdef"[num % base];

        num /= base;

    } while(num != 0);

    return p;

}

  
    
the result is 11011010110101001000110011111101001011000100011001100111110001


你可能感兴趣的:(函数)