Advertisement
defineSN

base conversion

Jul 16th, 2012
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.36 KB | None | 0 0
  1. /*this program converts number in any base to any other base (limit>> only upto hex number, ie, base 16)*/
  2.  
  3.  
  4. #include<stdio.h>
  5. #include<conio.h>
  6. #include<stdlib.h>
  7.  
  8.  
  9. int entry();// gets any base input and converts it to decimal
  10.  
  11. int main(void)
  12. {
  13.  
  14. int ip_num,ip_base,op_num,op_base,i=0,cont_flag,acc[20];
  15.  
  16. char base_digits[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  17.  
  18. do
  19. {
  20. ip_num=entry();
  21.  
  22. printf("choose the base to convert to:");
  23. scanf("%d",& op_base);
  24.  
  25.  
  26. puts("\nin main()\n\nexecuting for() loop . . .\n\n");
  27. for(i=0;i<20 && ip_num!=0;i++) // converts to i/p to required o/p base
  28. {
  29.  
  30. acc[i]=(ip_num % op_base);
  31. ip_num=ip_num/op_base;
  32. printf("index: %d \n",i);
  33.  
  34. if(ip_num==0)
  35. break;
  36.  
  37. if(i==19) {
  38. puts("array acc[] full");
  39. break;
  40. }
  41.  
  42. }//end of for()
  43.  
  44. printf("\nconverted number in base %d is:",op_base);
  45.  
  46. for(i;i>=0;i--)// prints the number to screen
  47. {
  48. printf("%c",base_digits[acc[i]]);
  49. }
  50.  
  51. printf("\n\nconvert another number? press\n 1(yes)\n 0(no)\n:");
  52. scanf("%d",& cont_flag);
  53. }while(cont_flag==1);// cont_flag lets user decide whether they want to convert another number or not
  54. }
  55.  
  56.  
  57. int entry()
  58. {
  59.  char sh[100],*stop;
  60.  int ip_num,ip_base;
  61. printf("Enter input number: ");
  62. scanf("%s",&sh);
  63.  
  64. printf("enter input base: ");
  65. scanf("%d",&ip_base);
  66.  
  67.  ip_num = strtol(sh,&stop,ip_base);
  68.  return(ip_num);
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement