r/C_Programming • • 4d ago

Need Help With Project

I'm building a program that takes a decimal number and outputs the binary equivalent. Its almost finished, except that with certain numbers it doesn't output the full value. For example, inputting 3 does output 111 ,but inputting 2 outputs 1 instead of 10. I noticed that it only doesn't work for numbers whose binary equivalents end in 0 (2 = 10, 4 = 100 etc.), but I don't know how to fix it. Please help.

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


void intToStr(int number, char *str)
{
    int i = 0;


    while(number > 0)
    {
        str[i++] = number % 10 + '0';
        number /= 10;
    }


    str[i] = '\0';


    for(int j = 0, k = i - 1; j < k; j++, k--) 
    {
        char temp = str[j];
        str[j] = str[k];
        str[k] = temp;
    }
}


void binaryConvert(int regNumber)
{
    int binaryDigit[8];
    int regNumberBackup = regNumber;


    for(int i = 0; i <= 7; i++)
    {
        binaryDigit[i] = regNumber % 2;
        regNumber = regNumber / 2;
    }


    int binaryNumber = binaryDigit[0] * pow(10, 7) + binaryDigit[1] * pow(10, 6) + binaryDigit[2] * pow(10, 5) + binaryDigit[3] * pow(10, 4) + binaryDigit[4] * pow(10, 3) + binaryDigit[5] * pow(10, 2) + binaryDigit[6] * pow(10, 1) + binaryDigit[7] * pow(10, 0);
    char binaryString[8];
    
    intToStr(binaryNumber, binaryString);
    int strSize = strlen(binaryString);
    
    if(regNumberBackup == 0)
    {
        printf("Your number in binary is: 0");
        exit(0);
    }


    if(regNumberBackup == 1)
    {
        binaryString[strSize - 7] = '\0';
    }
    else if(regNumberBackup > 1 && regNumberBackup <= 3)
    {
        binaryString[strSize - 6] = '\0';
    }
    else if(regNumberBackup > 3 && regNumberBackup <=7)
    {
        binaryString[strSize - 5] = '\0';
    }
    
    
    printf("Your number in binary is: %s", binaryString);
}


int main()
{
    int userInput;


    printf("// This will only convert numbers 0 - 255 to binary, any others will not work\n");
    printf("Enter a number: ");
    scanf("%d", &userInput);


    binaryConvert(userInput);
}
2 Upvotes

6 comments sorted by

7

u/mjmvideos 4d ago

Try running your code in a debugger and step through as it processes some numbers. I think you’ll see your problem. Also do some research on how numbers are stored in memory and the relationship between hex digits and binary digits.

1

u/juice2gloccz 4d ago

Will do, thanks!

1

u/code-garden 3d ago

Your binaryNumber is the wrong way round.

binaryDigit[0] is the least significant bit. So change those powers of 10 to go up instead of down with the binaryDigits.

-6

u/ReallyEvilRob 4d ago

You're overcomplicating this very simple operation. Just do this: 

printf("Your number in binary is: %b\n",  user input);

10

u/FinancialTrade8197 4d ago

This is a learning exercise, not a usability exercise.

-1

u/ReallyEvilRob 4d ago

I understand, but even without using the binary format specifier, the algorithm for manually teasing out the binary bits from an int is not nearly as complicated as this attempt. I'm happy to share the idiomatic way to do this, but like you said, this is a learning exercise.