473,412 Members | 2,012 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,412 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 57528

"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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.