473,785 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 57569

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

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

"Andrew" <ad*****@dccnet .com> wrote in message news:vn******** ****@corp.super news.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.super news.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(unsi gned 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.empt y()) result += "0";
}
return result;
}

Jul 19 '05 #5

"Andrew" <ad*****@dccnet .com> wrote in message
news:vn******** ****@corp.super news.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<typena me T>
std::string xxx_to_bin(cons t T& value)
{
const std::bitset<std ::numeric_limit s<T>::digits + 1> bs(value);
const std::string s(bs.to_string( ));
const std::string::si ze_type pos(s.find_firs t_not_of('0'));
return pos == std::string::np os ? "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.newshosti ng.com...

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

news:vn******** ****@corp.super news.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
15174
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 text as it also appears in the text file. It's been a long time since I've written any VB code. What am I doing wrong? (The default value of modulus is 255.) Private Sub cmdStart_Click() 'samples an image at intervals depending on the settings
21
3114
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 bit by bit to another byte. They have some weird way they set this up that i have to compare these things with AND. in other words, if bit 1 is 1 AND bit 1 is 1 then the real value is 1... long story short. is there a good way to compare...
12
5928
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: <gibberish>file//g:\pathtofile1<gibberish>file//g:\pathtofile2<gibberish> etc. I want to remove the "g:\" from the file paths. I wrote a console app that successfully reads the file and writes a duplicate of it, but fails for some reason to do the "replacing" of the "g:\". The code...
6
2732
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, every now and then the client would timeout and have to re-initiate the request... TIA!
21
3836
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
4159
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 whatever? I would analyze it for myself if someone would provide me with some binary (hex) representations of some numbers as: 0, 1, -1 and couple more of your choice.
7
19233
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 00000000000000001111111111111111 11111111111111111111111111111111 00000000000000000000000000000000 11111111111111110000000000000000
3
3198
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 have to use: int main(int argc,char** argv) which is archaic. My compiler is Visual Studio 5.0 Pro. To make my dilemma as clear as possible:
0
10327
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10151
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8973
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.