Click here to Skip to main content
Rate this: bad
good
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 4:21am
Comments
Sergey Alexandrovich Kryukov at 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
Rate this: bad
good
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.
  Permalink  
Rate this: bad
good
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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 404
1 Nirav Prabtani 338
2 Abhinav S 170
3 PIEBALDconsult 160
4 Dave Kreskowiak 155
0 OriginalGriff 7,580
1 Sergey Alexandrovich Kryukov 6,806
2 Maciej Los 3,919
3 Peter Leow 3,693
4 CHill60 2,742


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 27 Feb 2013
Copyright © CodeProject, 1999-2014
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