455,514 Members | 1,728 Online 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
4 Replies

 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<

 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

 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. What is 123 % 10 (that is if I divided 123 by 10, what would the remainder be)? Write this number down. Now, divide 123 by 10. What is the integer result of that operation? Lets assign that value to a variable x. Now, what is x % 10? Write this number down. Let x be x / 10 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 