473,387 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Integer to Binary

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


Thanks in Advance

Cheers
Jul 19 '05 #1
6 57525

"Andrew" <ad*****@dccnet.com> wrote in message news:vn************@corp.supernews.com...
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?
Jul 19 '05 #2
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
"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f***********************@news.newshosting.co m...

"Andrew" <ad*****@dccnet.com> wrote in message news:vn************@corp.supernews.com...
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?

Jul 19 '05 #3
"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f***********************@news.newshosting.co m...

"Andrew" <ad*****@dccnet.com> wrote in message news:vn************@corp.supernews.com...
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?


I think he wants a string representation of the binary data...
Jul 19 '05 #4

"Andrew" <ad*****@dccnet.com> wrote in message news:vn************@corp.supernews.com...
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;
}

Jul 19 '05 #5

"Andrew" <ad*****@dccnet.com> wrote in message
news:vn************@corp.supernews.com...
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 that would be just as helpful

I am sorry if I wasn't clear

Thanks for the quick Reply

Andrew
"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f***********************@news.newshosting.co m...

"Andrew" <ad*****@dccnet.com> wrote in message

news:vn************@corp.supernews.com...
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?


Jul 19 '05 #6
Andrew wrote:
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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Elliot Marks | last post by:
I want to output this data to text and binary files. The binary file contains the garbage you would expect to see if you try to read it with a text editor, but below that the output appears as...
21
by: nephish | last post by:
i have an interesting project at work going on. here is the challenge. i am using the serial module to read data from a serial input. it comes in as a hex. i need to make it a binary and compare it...
12
by: Adam J. Schaff | last post by:
I am writing a quick program to edit a binary file that contains file paths (amongst other things). If I look at the files in notepad, they look like: ...
6
by: | last post by:
Hi all, is there a better way to stream binary data stored in a table in sql 2005 to a browser in .net 2.0? Or is the code same as in .net 1.1? We noticed that in certain heavy load scenarios,...
21
by: nicolasg | last post by:
does anyone know a module or something to convert numbers like integer to binary format ? for example I want to convert number 7 to 0111 so I can make some bitwise operations... Thanks
3
by: shyha | last post by:
Hello! Does anybody know what is binary representation of integer datatype fields written to archlogs on z/OS (OS/390) machines? Is it "Two's complement", "One's complement", Sign-modulo or...
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
3
by: nguser3552 | last post by:
Hello Everyone, I have a problem I can't surmount, anything is gravy at this point. I need to be able to read any type of file .ext (mov,mpeg,mp3,etc) in binary format. I can do this in C, but ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.