Convert decimal/binary/hex in C++. how ?
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
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).
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).
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).
Quote:Original post by ZahlmanWhere is std::bin defined? I only am aware of hex, oct and dec.Quote:Original post by SB4Use 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).
Can you explain how we convert from decimal to binary, please?
What exactly are you trying to achieve? Your question is vague...
To print an integer as hex:
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):
Note: a more efficient way [for 32 bit integers] would be:
Is this what you were asking for...?
[Edited by - Jan-Lieuwe on December 1, 2008 8:00:55 PM]
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]
Quote:Original post by SB4Repeatedly 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?
Ok, i want to convert from decimal to binary. how?
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.
i wrote the code for convert from decimal to binary:
Now Is this a good code or is there a better way to do the same work ?
#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 ?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement