/********************************************* * This program converts a number in base a * * into a number in base b (a and b must be * * between 2 and 36). * * ------------------------------------------ * * Ref.: "Mathématiques en Turbo-Pascal By * * M. Ducamp and A. Reverchon (2), * * Eyrolles, Paris, 1988" [BIBLI 05]. * * ------------------------------------------ * * Sample runs: * * * * BASE CONVERSION * * * * Start Base (2 to 36): 5 * * Arrival Base (2 to 36): 3 * * * * Enter number in start base: 3421 * * In base 3: 200000 * * Enter number in start base: 3420 * * In base 3: 122222 * * * * * * BASE CONVERSION * * * * Start Base (2 to 36): 10 * * Arrival Base (2 to 36): 16 * * * * Enter number in start base: 65535 * * In base 3: FFFF * * Enter number in start base: 100 * * In base 3: 64 * * * * BASE CONVERSION * * * * Start Base (2 to 36): 16 * * Arrival Base (2 to 36): 2 * * * * Enter number in start base: FF * * In base 3: 11111111 * * Enter number in start base: 3A * * In base 3: 111010 * * Enter number in start base: E2 * * In base 3: 11100010 * * * * Note: Enter 0 to exit. * * * * C++ version by J-P Moreau * * (www.jpmoreau.fr) * ********************************************** Explanations: ------------ The letters A,B,...,Z represent 10,11,...,36 in the base > 10. The number is first converted from base a to base 10 by Function DecodeBase, then converted from base 10 to base b by Function CodeBase. */ #include #include #include #include #define MAXREAL 1e40 #define FALSE 0 #define TRUE 1 int ba,bd; char x[40],y[40]; float r; //Convert a number from base b to base 10. The function returns //FALSE if b not in [2..36] or if string x contains invalid //characters in base b or if result y is too big. bool DecodeBase(char *x,int b, float *y) { int lgth,i,j; float mult; char *ch; if (b<2 || b>36) { printf(" Base must be between 2 and 36 !\n"); return FALSE; } *y=0; mult=1.0; lgth=strlen(x); for (i=0; i=b) return FALSE; *y+=mult*j; if (mult>MAXREAL/b) return FALSE; mult=mult*b; } return TRUE; } //Convert a number from base 10 to base b. The function returns //FALSE if b not in [2..36] or if string x contains invalid //characters in base 10 or if number x is too big. bool CodeBase(float x,int b, char *y) { int n; char ch[2]; if (b<2 || b>36) { printf(" Base must be between 2 and 36 !\n"); return FALSE; } strcpy(y,""); while (x>0) { n=(int) floor(x-b*int(x/b)); if (n<10) { //y=Chr(Ord("0")+n)+y; sprintf(ch,"%d",n); strcat(y,ch); } else { //y=Chr(Ord("A")+n-10) +y; switch(n) { case 10: strcat(y,"A"); break; case 11: strcat(y,"B"); break; case 12: strcat(y,"C"); break; case 13: strcat(y,"D"); break; case 14: strcat(y,"E"); break; case 15: strcat(y,"F"); break; case 16: strcat(y,"G"); break; case 17: strcat(y,"H"); break; case 18: strcat(y,"I"); break; case 19: strcat(y,"J"); break; case 20: strcat(y,"K"); break; case 21: strcat(y,"L"); break; case 22: strcat(y,"M"); break; case 23: strcat(y,"N"); break; case 24: strcat(y,"O"); break; case 25: strcat(y,"P"); break; case 26: strcat(y,"Q"); break; case 27: strcat(y,"R"); break; case 28: strcat(y,"S"); break; case 29: strcat(y,"T"); break; case 30: strcat(y,"U"); break; case 31: strcat(y,"V"); break; case 32: strcat(y,"W"); break; case 33: strcat(y,"X"); break; case 34: strcat(y,"Y"); break; case 35: strcat(y,"Z"); } } x=(float) int(x/b); } y=strrev(y); //string inversion return TRUE; } void main() { printf("\n BASE CONVERSION\n\n"); printf(" Start Base (2 to 36): "); scanf("%d",&bd); printf(" Arrival Base (2 to 36): "); scanf("%d",&ba); strcpy(x,"1"); while (strcmp(x,"0")!=0) { printf("\n Enter number in start base: "); scanf("%s",x); if (strcmp(x,"0")!=0) { if (DecodeBase(x,bd,&r)) { if (CodeBase(r,ba,y)) printf(" In base %d: %s\n",ba,y); else printf(" Error in coding number.\n"); } else printf(" Error in decoding number.\n"); } } printf("\n\n"); } //end of file basis.cpp