#include <iostream>
#include <String>
#include <bitset>
usingnamespace std;
void DecToBin(int num, int base);
double rem;
int final[99];
int i = 0.0; // look at this beautiful face.
char store;
int main()
{
double DecimalNum, base;
cout << "Welcome to Binary conversion program!" << endl;
cout << "Enter your base(Enter your base 2~35): ";
cin >> base;
while (base < 2 || base > 35)
{
cout << "Between 2~35 please :)" << endl;
while(!(cin >> base)) // Loops that goes on until cin fails
{
cin.clear(); // if fails, clear the failed flag.
while(cin.get() != '\n'){} // A loop the cleans out anything that is left over in the input stream.
cout << "Only numbers please: " << endl << "What's the base again? " <<endl;// Output failed prompt. And requires only numbers
}
}
cout << "Enter number in decimal: ";
cin >> DecimalNum;
DecToBin(DecimalNum, base); // Store the numbers.
return 0;
}
/////////////////////////////////////
void DecToBin(int num, int base)
{
while (num != 0)
{
rem = (num%base); //Finding remainder
final[i] = rem; //binary
i++;
num = num/base;
}
for (i--; i>-1; i--)
{
if (final[i]<10) // if the remainder goes up to 10 then it changes the value to letters..
cout << final[i];
else
cout << char(final[i]-10+'a'); // If the remainder goes up to 10, the value will changes to letters.
}
cout << endl;
}
I'm not sure I understand the question. Mixing double/int like you're doing is asking for trouble as well as causing annoying errors. Every double in your code can be an int.
Now how do I store 11011110(or the binary values) and change that to hexadecimal?
Every number entered by a user is value. There' no such thing as a binary value or a hexidecimal value. Those are merely different ways of representing the same thing.
int num literally is binary data. And of course you could get the hexidecimal representation by calling your inaccurately named DecToBin function with DecToBin(num, 16).
The value is always stored as binary. That's just how computers work. In order to display these sort of things, you just need to modify the outputs:
To display decimal, octal and hexadecimal is easy:
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
int main()
{
int a;
std::cout << "Enter a number: ";
std::cin >> a;
std::cout << "Dec: " << std::dec << a << std::endl;
std::cout << "Hex: " << std::hex << a << std::endl;
std::cout << "Oct: " << std::oct << a << std::endl;
return 0;
}
Enter a number: 53
Dec: 53
Hex: 35
Oct: 65
Binary is a little tougher since in a non-academic environment binary is pretty useless as it's just too annoying to read. Hexadecimal is much easier if you need to read bits. See this post for a working solution: http://cplusplus.com/forum/beginner/64805/
EDIT: Oh I see, you want to also be able to input any number! Give me a moment, I'll see if I can get something working. You'll need to modify the number upon entry to get this working.
int char2num(char c)
{
if (c >= '0' && c <= '9') return c-'0'; // '1' is 1 , '2' is 2 , etc...
if (c >= 'a' && c <= 'z') return c-'a'+10; // 'a' is 10, 'b' is 11, etc...
if (c >= 'A' && c <= 'Z') return c-'A'+10; // 'A' is 10, 'B' is 11, etc...
return -99999; // Returning a crazy number so that we know there was a problem
}
int string2num(string word, int base)
{
int output = 0, base_factor = 1;
for (int i = word.size()-1; i >= 0; i--) // Start at least significant digit
{
output += char2num(word[i]) * base_factor;
base_factor*=base;
}
return output;
}
int main()
{
string in;
int base, num;
cout << "Enter a base: ";
cin >> base;
cout << "Enter a number: ";
cin >> in;
num = string2num(in,base);
// Do whatever you want with num now (i.e. output in dec, hex, oct, bin)
return 0;
}
#include <iostream>
#include <string>
usingnamespace std;
int char2num(char c)
{
if (c >= '0' && c <= '9') return c-'0'; // '1' is 1 , '2' is 2 , etc...
if (c >= 'a' && c <= 'z') return c-'a'+10; // 'a' is 10, 'b' is 11, etc...
if (c >= 'A' && c <= 'Z') return c-'A'+10; // 'A' is 10, 'B' is 11, etc...
return -99999; // Returning a crazy number so that we know there was a problem
}
int string2num(string word, int base)
{
int output = 0, base_factor = 1;
for (int i = word.size()-1; i >= 0; i--) // Start at least significant digit
{
output += char2num(word[i]) * base_factor;
base_factor*=base;
}
return output;
}
int Bit (int x, short pos)
{
return (0x00000001 & (x >> (pos-1)));
}
string num2Bin(int num)
{
string output;
for (int i = 32; i > 0; i--)
output += (Bit(num, i)+'0');
return output;
}
int main()
{
string in;
int base, num;
cout << "Enter a base: ";
cin >> base;
cout << "Enter a number: ";
cin >> in;
num = string2num(in,base);
cout << "Dec: " << dec << num << endl;
cout << "Hex: " << hex << num << endl;
cout << "Oct: " << oct << num << endl;
cout << "Bin: " << num2Bin(num) << endl;
return 0;
}