Chat LIVE With Programming Experts! There Are 23 Online Right Now...

 

Code Snippets

  

C Source Code


Welcome to Dream.In.Code
Become an Expert!

Join 244,215 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,476 people online right now. Registration is fast and FREE... Join Now!





Converting Decimal To other base up to 16

Converts Decimal to Other Bases from 2 to 16 and displays it. [Both Iterative And Recursive]

Submitted By: AmitTheInfinity
Actions:
Rating:
Views: 4,438

Language: C

Last Modified: February 7, 2007

Snippet


  1. #include <stdio.h>
  2.  
  3. void convertToBase(int, int);
  4. void convertToBaseRec(int, int);
  5. void main()
  6. {
  7.      int base,number;
  8.      // the number to be converted
  9.      printf("Enter The Number : ");
  10.      scanf("%d",&number);
  11.      // accept only valid based that is from 2 to 16
  12.      while(base>16 || base<2)
  13.      {
  14.           printf("Enter The Base : ");
  15.           scanf("%d",&base);
  16.      }
  17.      //iterative
  18.       printf("\n Output Of Iterative Conversion : ");
  19.      convertToBase(base,number);
  20.       //recursion
  21.       printf("\n Output Of Recursive Conversion : ");
  22.      convertToBaseRec(base,number);
  23. }
  24.  
  25. //convert and print the answer
  26.  
  27. void convertToBase(int base, int number)
  28. {
  29.      //this array contains all digits that are used in different bases til 16 [hex]
  30.      char digits[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
  31.      char output[100];
  32.      int i=0;
  33.      //actual logic
  34.      while(number>0)
  35.      {
  36.           //copy character from array digit situated at index = reminder of number / base
  37.           output[i++] = digits[number%base];     
  38.           number = number/base;
  39.      }
  40.      i--;
  41.      //print output
  42.      for(;i>=0;i--)
  43.      {
  44.           printf("%c",output[i]);
  45.      }
  46. }
  47.  
  48. void convertToBaseRec(int base, int number)
  49. {
  50.     //this array contains all digits that are used in different bases til 16 [hex]
  51.      char digits[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
  52.      //actual logic
  53.      if(number<=0)
  54.      {
  55.           return;
  56.      }
  57.      //Call Recursively
  58.      convertToBaseRec(base,number/base);
  59.      //print character from array digit situated at index = reminder of number / base
  60.      printf("%c",digits[number%base]);     
  61.      
  62. }

Copy & Paste


Comments


npsnet 2009-03-08 00:23:58

Is there any C code snippet which will directly convert Binary format string to Hexadecimal string ? plz let me know


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month