Undefined
By: awegawe | Date: Nov 27 2007 05:38 | Format: C++ | Expires: never | Size: 1.08 KB | Hits: 2061
- #include <iostream>
- #include <string>
- using namespace std;
- string bin2dec(int num)
- {
- string bit = " ";
- bit[0] =('0' + (num % 2));
- if (num / 2)
- return ( bin2dec(num/2) + bit );
- else
- return bit;
- }
- string convert(int num, int minlen)
- {
- int worknum = num;
- string retval;
- if (num < 0)
- {
- worknum = 0 - worknum;
- retval = bin2dec(worknum);
- //do some 2's complement foo
- }
- else
- retval = bin2dec(worknum);
- while (retval.length() < minlen)
- retval = "0" + retval;
- return retval;
- }
- int main()
- {
- int minlen;
- int num;
- string superduper;
- cout << "min len: ";
- cin >> minlen;
- cout << endl << "num to convert: ";
- cin >> num;
- superduper = convert(num, minlen);
- cout << endl << superduper << endl;
- return 0;
- }
Latest pastes
1 hours ago
11 hours ago
1 days ago
2 days ago
2 days ago