Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hello please help me here I need to convert base 10 integer to binary coded base 2 in c++ i really don't know where to start
Posted
Comments
Sergey Alexandrovich Kryukov 11-Apr-13 18:06pm    
Please don't post non-answers as "solution". It can give you abuse reports which eventually may lead to cancellation of your CodeProject membership.
Comment on any posts, reply to available comments, or use "Improve question" (above).
Also, keep in mind that members only get notifications on the post sent in reply to there posts.

You already got 3 abuse report on your non-answer. You don't want it: if wrong posting start annoy people, they may report the account, which is worse. Just be careful about it.

Thank you for understanding.
—SA

Break it into two steps:

1. convert base 10 integer to binary coded base 2
I hope you know (you should if you are asked to code it!) how to convert a decimal number into binary.... (Hint: Keep dividing by 2 and remainders make binary number)

2. Code the conversion step in C++
Once you are done with 1, you will see it's straight forward coding for conversion. Keep account of remainders to read all at once at end.


Try out. Post specific issue if you face any.
 
Share this answer
 
Here is a C# .NET example that does it. You should be able to convert this to C++.

The bolded line below converts an integer to a string that contains the binary representation of the integer.
int intValue = 10; // Original Integer value to be converted
string binValue = Convert.ToString(intValue, 2); // Integer value represented as a string of binary digits
int intBackToOriginalValue = Convert.ToInt32(binValue, 2); // Binary value converted to Integer
MessageBox.Show("bin: " + binValue.ToString() + Constants.vbNewLine + "int: " + intValue.ToString() + Constants.vbNewLine + "back" + intBackToOriginalValue.ToString());


See Convert.ToString Method (Int32, Int32)[^]. The Microsoft documentation shows that Convert.ToString can be used in C++ but does not provide an example.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900