0

my binary conversion doesn't work after it recurs a second time, it seems to work only during the first time through. The purpose of the is have a user input a number to convert to Hex, Octal, and brinary from a integer and keep on asking and converting until the user inputs 0. Please help!

#include <stdlib.h>
#include <stdio.h>
#include <math.h>

long toBinary(int);

int main(void) {
    int number = 0;
    long bnum;   
    int zero = 0;

    while(number != zero) {
        puts("\nPlease enter a number you would like to convert to");
        puts("\nHexadecimal, octal, and binary: ");
        scanf("%d", &number);

        if(number != zero) {
            printf("\nThe Hexadecimal format is: %x", number);
            printf("\nThe Octal format is: %o", number);
            bnum = toBinary(number);
            printf("\nThe binary format is: %ld\n", bnum);
        }
        else {
            puts("\nI'm sorry you have to enter a number greater than 0.\n");
            puts("\nOr have enter an invalid entry.");

        }
    }
    return 0;
}   

long toBinary(int number) {
    static long bnum, remainder, factor = 1;
    int long two = 2;
    int ten = 10;

    if(number != 0) {
        remainder = number % two;
        bnum = bnum + remainder * factor;
        factor = factor * ten;
        toBinary(number / 2);
    }
    return bnum;
}       
6
  • Why have int zero = 0 is going to be a mystery to all of us? You do know that computers are binary machines in the first place. There is no conversion just a different representation Commented Feb 22, 2014 at 6:21
  • Curious to know how 32 0's or 1's can be represented in a long Commented Feb 22, 2014 at 6:25
  • I'm new to c programming i just started using c like 2 weeks ago so i'm really fresh to this so i'm not sure. Commented Feb 22, 2014 at 6:29
  • So are you saying should change long to something else like a unsigned int or just int? Commented Feb 22, 2014 at 6:34
  • In case of binary it is better to use array of characters. see the answer posted below Commented Feb 22, 2014 at 6:36

3 Answers 3

1

You just need a function to convert an integer to its binary representation.

Assuming the int is 32 bits then this should work:

#include <stdio.h>

int main()
{
    char str[33];
    str[32] = 0;
    int x = 13, loop;
    for (loop = 31; loop >= 0; --loop) {
       str[loop]  = (x & 1) ? '1' : '0';
       x = x >> 1;
    }
    printf("As %s\n", str);
    return 0;
}

You can make this into a function, read x etc...

EDIT

For octal/hex - printf will do this for you

EDIT

Here goes recursively

#include <stdio.h>

void PrintBinary(int n, int x) {
   if (n > 0) {
      PrintBinary(n - 1, x >> 1);
   }
   printf("%c",(x & 1) ? '1' : '0');
}

int main()
{
   PrintBinary(32,12);
   return 0;
}
Sign up to request clarification or add additional context in comments.

3 Comments

I think you meant loop = 31, 32 to 0 makes 33:)
Thank you, but I'm trying not to use an array, I'm trying to do it recursively.
... Btw you are effectively using the calling stack as the array
0

First of all I am surprised it even works once. Firstly, your while condition is while number does not equal zero. But right off the bat, number equals 0 and zero equals 0. Therefore the while should never run. If you want to keep this condition for the main loop, change it to a do-while loop: do { //code } while (number != zero);. This will run the code at least once, then check if the inputted number doesn't equal zero. That brings me to the next issue; your scanf for number is scanning for a double and placing it in a regular integer memory spot. Quick fix: scanf("%i",&number);. Also I am finding some functions called puts.. I find it best to keep with one printing function, printf. Now, I am finding afew errors in your toBinary function, but if it works than I guess it works. These are all the errors i could find, I hope this helped. But for future reference there is no need to declare a variable for a const number like 2 or 10 at this level.

2 Comments

Thanks for the feed back, i set number to zero because i was trying to find the error with the second recursion, when i try to a second conversion by binary conversion comes out wrong
Your welcome. And about your binary conversion, you are going to need a loop. Inside the loop you should have a way to determine the size of number that hasn't been changed to binary and based on the size check if the remainder is 0 or not. i.e for (i=8;i!=-1;i--) { x = pow(2,i); if (number > x) { if (number%x==0) printf("0"); else printf("1"); number=-x; } }
0
#include <stdint.h>

char* toBinary(int32_t number, int index){
    static char bin[32+1] = {0}, *ret;
    if(index == 32){
        memset(bin, '0', 32);
        return toBinary(number, 31);
    } else if(number & (1<<index))
        bin[31-index] = '1';

    if(index)
        (void)toBinary(number, index-1);
    else
        for(ret = bin; *ret == '0';++ret);

    return ret;
}

...
int number = -1;
...
printf("\nThe binary format is: %s\n", toBinary(number, 32));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.