Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int main(int argc, char *argv[]){
- if(argv[1] == NULL){
- printf("\nEnter an argument to the program!\n");
- exit(8);
- }
- int in;
- short i, j; //iterator
- char b[12];
- if(sscanf( argv[1], "%d", &in ) == 1);
- else{
- printf("Enter an integer for the argument!\n");
- exit(8);
- }
- //only have to calculate binary and 32 bit, the rest is automatic!
- for(i = 11; i >= 0; i--){
- if((1 << i) & in)
- b[11 - i] = '1';
- else
- b[11 - i] = '0';
- }
- b[12] = 0;
- char b32[32]={"0123456789abcdefghijklmnopqrstuv"};
- i = j = 0; // reset iterators
- char b32result[32]; // the final conversion
- while(in){
- b32result[i] = b32[in%32];
- in = in/32;
- i++;
- }
- //notice the fact you can use %d, %x %o for decimal, hex and octal automatically.
- printf("\nDecimal: %d\nBinary: %s\nOctal: %o\nHex: %x\nBase32: %s\n", in, b, in, in, b32result);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement