C program to convert a decimal number to binary string

Sulaiman Mutawalli :

I was given this program to convert a inputted decimal number to a string which is binary number. Now this is a correct program and does work. I have a doubt in while loop the expression saying ch = (rem + 48); I believe value of remainder rem is ultimately going to be inherited by ch then why adding 0 ie. 48 in ascii terms is making difference. what is making addition of 48 convert rem to character form. If I just write ch = rem;, ch is not considered as character and adding 48 makes it charcter. But Why???

void main()
{
    char x[15],tmp,ch;
    int i=0,j=0,dno,rem;
    printf("\nEnter decimal number:");
    scanf("%d",&dno);
    while(dno>0)
    {
        rem = dno % 2;
        ch = (rem+48);
        x[j++] = ch;
        dno/=2;
    }
    x[j--]='\0';
    while(i<j)
    {
        tmp = x[j];
        x[j--] = x[i];
        x[i++] = tmp;
    }
    puts(x);
    return;
}
Paolo :

ch is a number that represents an ascii character value

48 correspond to the character "0"

49 correspond to the character "1"

the sum of rem (0 or 1) to 48 leads to 48 or 49, respectively the characters "0" and "1"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to convert binary string to decimal?

How to convert binary string value to decimal

How to convert a decimal number to binary in Swift?

Convert binary string to decimal

Decimal to Binary convert in C

Convert negative binary number to decimal

decimal number convert to 16bit binary (C)

Convert a number string to a rounded decimal (C#)

javascript program to convert binary to decimal and back

C program to convert Binary to Hexadecimal number system

Program to convert octal to binary number system

Need to convert string of 1s and 0s as a binary representation into a decimal number

binary string to decimal number in c

Simple function to convert decimal number to binary in Python

Program does not terminated properly convert binary to decimal

JavaScript - Convert decimal number to 4-bit binary number in the form of String

Arduino C/C++ Convert binary string to decimal

Convert Large Binary String to Decimal in Java

Convert 32 bit binary string of 1's and 0's to Signed Decimal Number in SQL Server

convert 32 bit, binary number to decimal in python

C++ convert binary to decimal from string input

C program to convert Decimal to Binary

Convert Hexadecimal string to decimal number in C++

My function to convert decimal number into binary in (C) is producing wrong output for (some numbers)! [Without Arrays]

Convert decimal to binary with billions number in C

I'm trying to program a decimal number into binary

SQL Convert string into a decimal number

How to make a binary to decimal program in C?

Convert decimal number provided in String to the binary