| | |
converting from binary to decimal and vice versa
![]() |
hello there, I am working on a small c program that automatically detects whether the input given is a decimal number or a binary number and coverts accordingly.
what is the best way to convert a binary number to a decimal number and a decimal number to a binary number.
i found this to be the easiest when converting from binary to deicmal
http://www.wikihow.com/Convert-from-Decimal-to-Binary
but my program doesnt work..?
what have i done wrong?
what is the best way to convert a binary number to a decimal number and a decimal number to a binary number.
i found this to be the easiest when converting from binary to deicmal
http://www.wikihow.com/Convert-from-Decimal-to-Binary
but my program doesnt work..?
c Syntax (Toggle Plain Text)
/*decimal to binary*/ #include <stdio.h> int main() { int dec; int hold=0; printf("decimal number : "); scanf("%d",&dec); hold=dec%2; while (hold!=1){ if (hold == 0){ printf("0"); }else if(hold >1){ printf("1"); }else{ printf("1"); } hold=hold%2; } }
Last edited by revenge2; Aug 10th, 2009 at 8:51 pm.
|--- A Thank you to all the Forum members ---| You don't have any divide's in there. You can't just use modulo for the whole thing. Also, you forgot to read the number backwards.
Last edited by Hiroshe; Aug 10th, 2009 at 9:32 pm.
"Sometimes, when I lie in bed at night and look up at the stars, I think to myself, "Man! I really need to fix that roof."-Jack Handy
•
•
Join Date: Jun 2008
Posts: 66
Reputation:
Solved Threads: 0
c Syntax (Toggle Plain Text)
/*decimal to binary*/ #include <stdio.h> void printBinary(const unsigned char val) { for(int i = 7; i >= 0; i--) if(val & (1 << i)) printf("1"); else printf("0"); printf("\n"); } int main() { int dec; int hold=0; printf("Integer number : "); scanf("%d",&dec); printBinary((unsigned char)dec); }
Ambarish
hmmm why doesn't this work?
i know i have to reverse the order but this doesnt work why is this?
c Syntax (Toggle Plain Text)
#include <stdio.h> void bin_c(int x); int main(){ int x; printf("Enter a number:"); scanf("%d",x); bin_c(x); return 0; } void bin_c(int x){ if (x == 1){ printf("1"); } while (x!=1){ int y; y=x/2; if (y == 0){ printf("0"); } if (y > 1){ printf("1"); } if (y == 1){ printf("1"); } bin_c(y); } }
i know i have to reverse the order but this doesnt work why is this?
Last edited by revenge2; Aug 12th, 2009 at 2:01 am.
|--- A Thank you to all the Forum members ---| To get a binary value from a decimal value
v = val %2; -- v will contain the value of the ones digit. Then, after you get the digit, remove it to get the next digit:v = val /2; Loop until val is 0. The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- HELP!!! Converting Hexadecimal to Decimal or vice versa (MS SQL)
- Converting Binary to Decimal (C++)
- How to convert Image into binary file and vice-versa (Java)
- Converting binary to decimal using recursion (C++)
- Binary And Decimal Conversions (C)
- [HELP]converting binary to decimal (C)
- Converting byte value into integer and vice versa (C++)
Other Threads in the C Forum
- Previous Thread: CRC Checksum Calculation
- Next Thread: Help with EvenOdd Function...
| Thread Tools | Search this Thread |
#include * adobe ansi array asterisks binarysearch centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc database dynamic execv feet fgets file floatingpointvalidation fork function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide inches include incrementoperators infiniteloop input interest intmain() iso kernel keyboard kilometer license linked linkedlist linux list locate looping lowest matrix meter microsoft mqqueue number oddnumber opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprograming socketprogramming standard strchr string suggestions systemcall test threads turboc unix urboc user variable voidmain() wab whythiscodecausesegmentationfault windowsapi






