Possible Duplicate:
Efficiently convert between Hex, Binary, and Decimal in C/C++
Im taking an Assembly Language class and was asked to write an application to accept a signed integer as input and output the corresponding 2's complement. I've been all over the internet trying to find code that would help, but the only thing that I can find is code that converts into exact binary (not the 16-bit format that I need with the leading zeroes). This is the code I have so far:
#include<iostream>
#include<string>
using namespace std;
string binaryArray[15] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
void toBinary(int);
void convertNegative();
int main()
{
cout << "This app converts an integer from -32768 to 32767 into 16-bit 2's complement binary format" << endl;
cout << "Please input an integer in the proper range: ";
int num;
cin >> num;
if (num < -32768 || num > 32767)
cout << "You have entered an unacceptable number, sorry." << endl;
if (num < 0)
{
toBinary(num);
convertNegative();
}
else
toBinary(num);
cout << endl;
system("pause");
return 0;
}
My toBinary function was the function you can find on the internet for decimal to binary, but it only works if I am outputting to the console, and it doesn't work on negative numbers, so I can't take the 2's complement. Any ideas?