• C

Simple Base Number Converter

I just started learning C programming and we just finished learning about function calls and base conversion.  Can you give me a simple program that converts a decimal based integer (user input) to binary, octal, and hexadecimal bases? It can't have printf %o (octal) or %x (hexidecimal).  Just a simple program for someone who just started learning about programming and learning about calling a function.

I'd really appreciate it.  Thanks alot!

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

few key points here,
1. all numbers are stored in binary format.
2. octal and hexadecimal are powers of 2.
3. to get bit by bit of a number, u can use the 'right-shift'( >> ) operation in C.
4. to divide a number by 2^n, right shift the number by n bits.

so here is the procedure to get the binary/octal/hexadecimal representation of the number.
keep this string of bits/octets/hex-bits
char *nbits="0123456789abcdef";

1. to get the binary representation of the number..
  right-shift bit by bit and keep storing/printing the bits one by one.
  (for each shifted bit print---> nbits[value_of_shifted_bit]; )
2. to get the octal representation of the number..
  right-shift the number by 3-bits at a time, and each time checking the number in the thus got 3 shifted bits, and print the appropriate octet.
  (for each shifted bit print---> nbits[value_of_shifted_bits]; )

3. for hexadecimal, shift the bits 4 at a time
  (for each shifted bit print---> nbits[value_of_shifted_bits]; )

the number that u'll print this way will be in the right-to-left direction ( reversed from normal left-to-right)

try implementing code on this and we will help you out to make it work.


ok let me give you an code-snippet example for printing the octal representation

#define OCT_MASK 0x07  //largest possible number represented by a single octal-digit(octet)
unsigned int num=0xffffffff;  // this is a sample 32 bit number
int nBits=3;  //number of bits to skip each time.. for octal it is 3, for hex it is 4, for binary it is 1.
int max_bits=8*sizeof(int); // number of bits in the integer on this platform
char *digit_char="0123456789abcdef";
int digit;

digit=num & OCT_MASK; // take the number in the right most 3 bits
num= num>>nBits; //drop of the last 3 bits

above loop will print the octets of num from right to left, to print them in the left-to-right order, u may keep storing the octets in the array...and print it later on

for hexadecimal representation use
#define HEX_MASK 0x0f
and nBits=4

for binary representation use
#define BIN_MASK 0x01
and nBits=1

change the other part of the codes accrodingly, try to bring together all this in a program, better will be to make a function which takes in the argument to choose from oct/hex/bin and return the appropriate character string
good luck
cooldeanAuthor Commented:
Hmm...thanks for the help but I'm having a hard time following your suggestions.  We haven't learned nBits.  We just started C Programming so I don't know how to use it, and what is 0x0f or 0x01.  I haven't learned using the size value like that.  I just learned it like: SIZE 100 or SIZE 50, but not with 0x0f.

Is there another simpler way?

Again, thanks alot.

Another method to convert to another base is to find the remainder when the number is divided by the base i.e. by 2, 8, 16 for binary, octal and hexadecimal.

sample code for binary would be somewhat like:

while (num > 0)
    x = num % 2;   //gets the remainder when number is divided by 2
    num = num / 2;
    array[i] = x + '0';       //array is character array so we have to add ascii value of 0 to store correctly

//Now reverse the array

for(j=0;j < i/2 ;j++)
    temp = array[j];
    array[j] = array[i-j-1];
    array[i-j-1] = temp;

Similarly you can do for octal and hex. For octal you have to divide by 8 and for hex by 16. For hex you have to check whether remainder is > 9. If it is you have to convert it to A,B,C,D,E,F and then store.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cooldeanAuthor Commented:
Thanks alot.

But got two questions:

Can you do this without using arrays?  I know it might be a longer way but how do you do it?  I tried to figure it out.

Second, when I tried to run your (Dhyanesh) program, I got errors saying that I need pointers for the arrays.  That's the reason I don't really want to use arrays.  My question is: I'm learning functions calls and if I add your program, would it be the function at the top and I would just need to call it at my void main function?

Again, I really appreciate your help.


if you wish to do without arrays it will be somewhat like:

binary = 0;
mult = 1;
while (num > 0)
   x = num % 2;   //gets the remainder when number is divided by 2
   num = num / 2;
   binary = mult *x + binary;       //binary is integer variable
   mult = mult * 10;

return binary;

However this will not work for hexadecimal as it involves A,B,C,D,E,F which require a char array to store.

>>would it be the function at the top and I would just need to call it at my void main function?

You can create a function on top of main or even after the end of main. If you create it before main you just need to call the function in main. However if you write function after closing brace of main you require a function prototype in the main before you call it i.e.

void main()
     int func1(int); //function prototype

    abc = func1(xyz);

int func1(int t)


another way to do it would be by using recursion.(function calling itself)

void binary(int n)
     if (n>0)

this function is called from the main.

similar for octal and hex. and for hex again you will have to check if remainder > 9 etc..

Try this program for a decimal(<255) to binary conversion......


typedef struct byte
      unsigned int fi : 1; //First bit denoted by fi.
      unsigned int se : 1; //Second bit denoted by se.
      unsigned int th : 1; //Third bit denoted by th.
      unsigned int fo : 1; //Fourth bit denoted by fo.
      unsigned int ff : 1; //Fifth bit denoted by ff.
      unsigned int si : 1; //Sixth bit denoted by si.
      unsigned int sv : 1; //Seventh bit denoted by sv.
      unsigned int ei : 1; //Eighth bit denoted by ei.
} byte;
byte test;

void byte_print(byte);
void convert_byte(int);

void main()
      int a;
//      struct byte test;
      printf("Enter the number < 255 to be converted to binary");
      scanf("%d", &a);

void convert_byte(int x)
      int i;
//      extern byte test;
      test.fi = x & 1;
      x >>= 1;
      test.se = x & 1;
      x >>= 1;
      test.th = x & 1;
      x >>= 1;
      test.fo = x & 1;
      x >>= 1;
      test.ff = x & 1;
      x >>= 1;
      test.si = x & 1;
      x >>= 1;
      test.sv = x & 1;
      x >>= 1;
      test.ei = x & 1;
      x >>= 1;

void byte_print(byte x)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.