Jump to content
  • Advertisement
Sign in to follow this  
SB4

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
thanks swiftcoder


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

Share this post


Link to post
Share on other sites
Quote:
Original post by SB4
thanks 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 this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Quote:
Original post by SB4
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).
Where is std::bin defined? I only am aware of hex, oct and dec.

Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by SB4
Ok, 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 this post


Link to post
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 this post


Link to post
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 ?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!