-
Notifications
You must be signed in to change notification settings - Fork 691
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #474 from santiago177/print-binary
Added intToBinary.cpp file, this code prints the binary representation of a given integer
- Loading branch information
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
int stringToInt(string s) { | ||
int p = 1; | ||
int n = 0; | ||
for(int i = s.size()-1; i >= 0; i --) { | ||
n += (s[i] - '0') * p; | ||
p *= 10; | ||
} | ||
return n; | ||
} | ||
|
||
string intToBinary(int n) { | ||
string bin = ""; | ||
while(n > 0) { | ||
bin = (char)((n % 2)+'0') + bin; | ||
n /= 2; | ||
} | ||
return bin; | ||
} | ||
|
||
int main(int argc, char** argv) { | ||
/*cout<<argv[1]<<endl; | ||
cout<<argc<<endl; | ||
cout<<stringToInt(argv[1])<<endl;*/ | ||
cout<<intToBinary(stringToInt(argv[1]))<<endl; | ||
return 0; | ||
} |