Integer to Binary

A

Andrew

Hi I have a question is there a function in C++ to convert an integer into a
Binary number




Thanks in Advance

Cheers
 
R

Ron Natalie

Andrew said:
Hi I have a question is there a function in C++ to convert an integer into a
Binary number
On nearly every computer capable of running C++, integers are already binary. Just
what exactly do you want to do?
 
A

Andrew

I want to have a user input an integer and then display that integer in its
Binary format

an example

I enter an integer 708 I would want the screen to display 10101 or whatever
the equivalent represenation in binary format would

As you can guess I don't quite have the math skills concerning this to be
able to do it myself so I was hoping there was a function of some sort to do
this or if someone could explain in laymens terms how to do this in Math
that would be just as helpful

I am sorry if I wasn't clear

Thanks for the quick Reply

Andrew
 
J

Jeremy Cowles

Ron Natalie said:
On nearly every computer capable of running C++, integers are already binary. Just
what exactly do you want to do?

I think he wants a string representation of the binary data...
 
R

Ron Natalie

Andrew said:
I want to have a user input an integer and then display that integer in its
Binary format

an example

I enter an integer 708 I would want the screen to display 10101 or whatever
the equivalent represenation in binary format would

#include <string>
#include <limits>
using namespace std;

string cvt_binary(unsigned int input) {
if(input == 0) return "0"; // trivial case
string result;
for(int i = numeric_limits<unsigned int>::digits() - 1; i >= 0; --i) {
if(input & (1 << i)) {
result += "1";
} else {
if(!result.empty()) result += "0";
}
return result;
}
 
M

Mike Wahler

Andrew said:
I want to have a user input an integer and then display that integer in its
Binary format

an example

I enter an integer 708 I would want the screen to display 10101 or whatever
the equivalent represenation in binary format would

As you can guess I don't quite have the math skills concerning this to be
able to do it myself so I was hoping there was a function of some sort to do
this

Using the standard library:

#include <bitset>
#include <iostream>
#include <limits>
#include <sstream>
#include <string>

template<typename T>
std::string xxx_to_bin(const T& value)
{
const std::bitset<std::numeric_limits<T>::digits + 1> bs(value);
const std::string s(bs.to_string());
const std::string::size_type pos(s.find_first_not_of('0'));
return pos == std::string::npos ? "0" : s.substr(pos);
}

int main()
{

std::cout << '\n';

for(int i = -5; i < 6; ++i)
std::cout << i << " decimal == "
<< xxx_to_bin(i) << " binary"
<< '\n';
return 0;
}

Note that the patterns you see with the negative values
might be different on your system, depending upon which
binary representation it uses.

-Mike





or if someone could explain in laymens terms how to do this in Math
 
K

Kevin Goodsell

Andrew said:
I want...

Please don't top-post. Read section 5 of the FAQ for posting guidelines.

http://www.parashift.com/c++-faq-lite/
to have a user input an integer and then display that integer in its
Binary format

an example

I enter an integer 708 I would want the screen to display 10101 or whatever
the equivalent represenation in binary format would

It depends somewhat on what binary representation you want. In
particular, how are negative values to be represented? As a one's
compliment or two's compliment value? Sign-magnitude? Or simply as a
minus sign followed by the magnitude in binary?
As you can guess I don't quite have the math skills concerning this to be
able to do it myself so I was hoping there was a function of some sort to do
this

No standard function, but they are generally easy to write.
or if someone could explain in laymens terms how to do this in Math
that would be just as helpful

This is not a math group, it is a C++ group. If you need an explanation
of binary numbers you should ask elsewhere. This may be useful:

http://www.wikipedia.org/wiki/Binary_numeral_system

-Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top