By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,514 Members | 1,728 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,514 IT Pros & Developers. It's quick & easy.

Decimal2binary conversion

P: 2
I require a c algorithm to convert 255 decimal numbers to binary using 8 bits. The binary numbers must print to screen on execution. Char's must be used as well as number, remainder, divisor & answer.

thanks sully1
May 29 '07 #1
Share this Question
Share on Google+
4 Replies


AdrianH
Expert 100+
P: 1,251
I require a c algorithm to convert 255 decimal numbers to binary using 8 bits. The binary numbers must print to screen on execution. Char's must be used as well as number, remainder, divisor & answer.

thanks sully1
It is good that you know the requirements, that will help you in figuring out the answer. So what is the problem you are having?

Please read the very first thread in the forum before posting again.


Adrian
May 29 '07 #2

P: 93
I require a c algorithm to convert 255 decimal numbers to binary using 8 bits. The binary numbers must print to screen on execution. Char's must be used as well as number, remainder, divisor & answer.

thanks sully1
In a for loop print the values from 0 to 255 with an integer variable.
Along with that try to display the integer in terms of Char

For eg: int i=32;
printf("%c",i); // char "a" will be displayed.
Same way You can print chars and int. To print binary equivalent
try this way (take the value in temp variable)
while(i>0)
{
cout<<i%2;
i/=2;
} // printing Divider, reminder, quotient all things can be done
//here

As a special case for the ASCII values between 48 to 57
[ these ASCII values represents 0 to 9 ]
check with the If condition whether value is between above range, if so
then deduct 48 from it to get decimal equivalent and then Convert it to
Binary.
Does this cheers you ?

If you are trying to get the data from user for which you need to give
binary value, then get everything as char Array bcz it may be number like
112 which is 3 digit decimal integer. Down cast to Exact decimal & then
proceed as usual, In case single char then proceed directly.
May 29 '07 #3

P: 2
It is good that you know the requirements, that will help you in figuring out the answer. So what is the problem you are having?

Please read the very first thread in the forum before posting again.


Adrian
ADRIAN

It's the algorithm that I am having trouble in writing, as none of the books that are available to me has any references for what I need, so that I can write it.

sully1
May 29 '07 #4

AdrianH
Expert 100+
P: 1,251
ADRIAN

It's the algorithm that I am having trouble in writing, as none of the books that are available to me has any references for what I need, so that I can write it.

sully1
To convert a number to a different base you need two arithmetic operators integer division ('/') and modulus ('%'). Say we are going to convert a base 10 number to a base 10 number. (Yeah, kinda stupid, but it is to get the point across). So the number we are going to convert is 123.
  1. What is 123 % 10 (that is if I divided 123 by 10, what would the remainder be)? Write this number down.
  2. Now, divide 123 by 10. What is the integer result of that operation? Lets assign that value to a variable x.
  3. Now, what is x % 10? Write this number down.
  4. Let x be x / 10
  5. Keep doing steps 3 and 4 until x is zero.

Note the numbers you have just written down. Also note the order. This method is the same for any base. Replace 10 with 2 for binary, 8 for octal, and 16 for hexadecimal.

Since you are converting to binary, it is not too difficult. As a hint, use an array to store the data prior to printing and don't forget the terminating '\0' character in your c-string.

Let me know if you have any other questions.


Adrian
May 29 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.