Binary Converter

Thought this would be useful.


// loadedjd july 15 2012
// binary converter
//sorry about indentation had copying issues
#include <iostream>
#include <cstdlib>
#include <string>
#include <windows.h>
using namespace std;
int main()
{


char answer;


int ans[8];
//user inpu
string user;
//increments of Binary code
int cpu[]={128,64,32,16,8,4,2,1};
cout << "Enter binary to convert \n";
getline (cin,user);
cin.ignore();
//collects user input

for(int j=7; j<=0; j++)
{
if(user[j]=='1')
{
ans[j]=cpu[j];
}
else
if(user[j]=='0')
{
ans[j]=0;
}
}
//checks the users input
int tot=ans[0]+ans[1]+ans[2]+ans[3]+ans[4]+ans[5]+ans[6]+ans[7];

cout << "Convert to int or char 'i' integer 'c' character \n";
cin >> answer;
//ask the user whether they want to convert the binary to a character or integer
if(answer=='i')
{
cout << tot <<endl;
}

else
if(answer=='c')
{
cout << char(tot) <<endl;
}




system("pause");
return 0;
}


Last edited on
Topic archived. No new replies allowed.