954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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..?

/*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;
	}
}

what have i done wrong?

revenge2
Junior Poster in Training
79 posts since Feb 2007
Reputation Points: 10
Solved Threads: 2
 

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.

Hiroshe
Posting Whiz in Training
256 posts since Jun 2008
Reputation Points: 431
Solved Threads: 17
 
/*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);	
}
ambarisha.kn
Junior Poster in Training
66 posts since Jun 2008
Reputation Points: 25
Solved Threads: 0
 

hmmm why doesn't this work?

#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?

revenge2
Junior Poster in Training
79 posts since Feb 2007
Reputation Points: 10
Solved Threads: 2
 

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 untilval is 0.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

could you pls. do me a program that converting a binary to decimal or decimal to binary with a simple codes in turbo c, which i can easily understand.. for example i will enter an number 1 then it will be automatically to convert it to its binary value. thank u! i will wait for your help! pls. help me..

akirue09
Newbie Poster
1 post since Mar 2010
Reputation Points: 8
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You