Click here to Skip to main content
12,747,162 members (30,140 online)
Rate this:
Please Sign up or sign in to vote.
See more: C++
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 27-Feb-13 5:21am
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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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.

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

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170215.1 | Last Updated 27 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100