Binary to Decimal Conversion in C - Input Size Issue -


I have to write a C program for one of my classes which changes the given binary number to the decimal. My program works for small inputs, but not for the big ones, I believe it can be due to the conversion specifier that I am using for scanf () but I am not positive my code is down

  # include & lt; Stdio.h & gt; # Include & lt; Math.h> int main (zero) {unsigned long inputNum = 0; Int currentBinary = 0; Int count = 0; Float decimal number = 0; printf ("a binary number input:"); scanf ("% lu", and InputNum); While (InputNum! = 0) {currentBinary = inputNum% 10; InputNum = InputNum / 10; Printf ("% d \ t% d \ n", current binary, input number); Decimal number = presentbread * pow (2, count); ++ count; } Printf ("decimal conversion:% .0f", decimal number); Return 0; }     39 9 333 3 39 913 3 39 9 1 1 39 9 393 3 decimal Decision: 5264    

When you do this for a large number, then inputNum

  current binary = input number% 10;   

The top part of it is "truncated" on the conversion to int if you want to live within a unnatural long , Then switch from currentBinary to unnatural long and use an unsigned long format specifier in printf . Apart from this, the unsigned long can not be large enough on many platforms, so you need to use the unsigned long time .

Better yet, switch to reading input in a string, validate it for nil and others (you have to do this anyway) and a cleaner character-rate- Conversion should be done in the manner of the letter This will let you go ahead with 64-bit of 19 binary digits for a full-scale int input.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -