# Convert decimal/binary/hex in C++. how ?

This topic is 3658 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hello GDev member, i want a program that will convert any of these numbers (binary, hexadecimal and decimal numbers) to the other two numbers in C++. for example if we choose the binary number should accept only 8-bits binary number as an input and returns the hexadecimal and decimal numbers equivelant to it. And the other selection will be decimal or hex. i know how to convert any of these numbers (binary, hexadecimal and decimal numbers) to the other two numbers manually but i don't know how to do that in C++. Any help would be appreciated Regards, Crimson

##### Share on other sites
Integers are just integers, so I assume you want to convert between string representations of these integers?

In C++, std::stream supports the manipulators std::hex, std::oct and std::dec, which allow you to read and write integers in hexadecimal, octal and decimal, respectively.

C (and by extension, C++) provides the function strtol(), which can read integers to any bases from 2-32.

And of course, reading/writing integers in the various bases are trivial to implement from scratch (character by character, multiply and add).

##### Share on other sites
thanks swiftcoder

Can you explain how we convert from decimal to binary, please?

##### Share on other sites
Quote:
 Original post by SB4thanks swiftcoder Can you explain how we convert from decimal to binary, please?

Use an input stream to read in decimal (which is default); then use an output stream to write in binary (using std::bin to specify that behaviour).

##### Share on other sites
Quote:
Original post by Zahlman
Quote:
 Original post by SB4Can you explain how we convert from decimal to binary, please?
Use an input stream to read in decimal (which is default); then use an output stream to write in binary (using std::bin to specify that behaviour).
Where is std::bin defined? I only am aware of hex, oct and dec.

##### Share on other sites
What exactly are you trying to achieve? Your question is vague...

To print an integer as hex:

printf("%08x", theDecimalNumber);

To print it as binary (C/C++ doesn't really have built in routines to do this for you, so things look ugly by default):

printf("%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u%u",  (theDecimalNumber >> 31) & 1,  (theDecimalNumber >> 30) & 1,  (theDecimalNumber >> 29) & 1,  (theDecimalNumber >> 28) & 1,  (theDecimalNumber >> 27) & 1,  [...]  (theDecimalNumber >> 0) & 1);

Note: a more efficient way [for 32 bit integers] would be:

char b[33];b[32] = '\0';for (int i = 0; i < 32; ++i){   b[31 - i] = '0' + ((theDecimalNumber >> i) & 1);}putstring(b);

Is this what you were asking for...?

[Edited by - Jan-Lieuwe on December 1, 2008 8:00:55 PM]

##### Share on other sites
Ok, i want to convert from decimal to binary. how?

##### Share on other sites
Quote:
 Original post by SB4Ok, i want to convert from decimal to binary. how?
Repeatedly spamming the same question is not going to get you anywhere. Plenty of information has been posted on this thread to allow you to find what you need, so how about you explain what you have tried (preferably with source code), and why it doesn't work?

##### Share on other sites
In real C++, there's std::bitset, which will do binary to/from internal conversion. Internal to/from Octal, Decimal is done with iostreams using manipulators.

##### Share on other sites
i wrote the code for convert from decimal to binary:
#include<iostream>#include<vector>using namespace std;void main(){	int decCounter, decNumber, Counter;	vector<int> binNumber;	Counter = 0;	cout << "Enter the Dec number: ";	cin >> decNumber;	for	(int i = 1; i <= decNumber; i*=2){		decCounter = i;	}		//cout << decCounter;	for(int i = decNumber; decCounter >= 1 ; decCounter /= 2){		if(i >= decCounter){			binNumber.push_back(1);			i -= decCounter;		}		else{			binNumber.push_back(0);			if(decCounter == 1){				break;			}		}				}	for(int i = 0; i < binNumber.size(); i++){		cout << binNumber;	}	cout << endl << "Press the enter key to exit";	cin.ignore(cin.rdbuf()->in_avail() + 1);}

Now Is this a good code or is there a better way to do the same work ?

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
12
5. 5
frob
12

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633669
• Total Posts
3013259
×