读书人

随意进制转换

发布时间: 2012-09-27 11:11:17 作者: rapoo

任意进制转换

/*任意进制转换*/#include <stdlib.h>#include <stdio.h>#define MAX 100int Convert(const char *a,int i,char *b,int j){    if(i<=0 || j<= 0)return 0;    char *p = a,*q = b;    int in=0,out=0,temp = 0;    while(*p != '\0')    {        if(*p <= '9')temp = *p - '0';        else temp = *p - 'A' + 10;        in = in*i + temp;        ++p;    }    while(in)    {        temp = in % j;        if(temp <= 9)            *q = temp + '0';        else *q = temp + 'A' - 10;        in /= j;        q++;    }    q--;    /*就地逆置*/        p = b;    char c;    while(q > p)    {        temp = *p;        *p = *q;        *q = temp;        q--;p++;    }    return 1;}int main(int argc, char* argv[]){    char *a = "1F";    char b[100] = {0};    int rst = Convert(a,16,b,10);    char *p = b;    while(*p)    {        printf("%c",*p);        p++;    }    return rst;}

读书人网 >编程

热点推荐