473,473 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Decimal2binary conversion

2 New Member
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
4 1777
AdrianH
1,251 Recognized Expert Top Contributor
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
Girish Kanakagiri
93 New Member
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
sully1
2 New Member
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
1,251 Recognized Expert Top Contributor
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

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

Similar topics

1
by: Stub | last post by:
Docs says that "The compiler does not use an explicit constructor to implement an implied conversion of types. It's purpose is reserved explicitly for construction." I put up code of three cases...
7
by: Michael Lehn | last post by:
Hi, I have a question regarding the conversion of objects. When is the conversion done by the constructor and when by the operator. My feeling tells me that the constructor is preferred. But...
11
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type...
2
by: Alex Sedow | last post by:
Why explicit conversion from SomeType* to IntPtr is not ambiguous (according to standart)? Example: // System.IntPtr class IntPtr { public static explicit System.IntPtr (int); public...
0
by: Lou Evart | last post by:
DOCUMENT CONVERSION SERVICES Softline International (SII) operates one of the industry's largest document and data conversion service bureaus. In the past year, SII converted over a million...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.