|
#include <stdio.h> |
|
#include <stdlib.h> |
|
#include <getopt.h> |
|
#include <string.h> |
|
#include <assert.h> |
|
|
|
#include "typeconvert.h" |
|
|
|
// https://linux.die.net/man/3/getopt |
|
|
|
|
|
void print_help(int error) |
|
{ |
|
if (error) |
|
printf("There was an error parsing the flags!\n\n"); |
|
|
|
printf("typeconvert: A useless-but-slightly-useful utility to convert one " |
|
"of binary, octal, decimal or hexadecimal to the other three\n\n"); |
|
printf("Usage: typeconvert [options] input\n"); |
|
|
|
printf("Options:\n"); |
|
printf(" --bin <binary_number>\t\tShow oct, dec and hex of a binary " |
|
"number\n"); |
|
printf(" --oct <octal_number>\t\tShow bin, dec and hex of an octal " |
|
"number\n"); |
|
printf(" --dec <decimal_number>\tShow bin, oct and hex of a decimal " |
|
"number\n"); |
|
printf(" --hex <hexadecimal_number>\tShow bin, oct and dec of a " |
|
"hexadecimal number\n"); |
|
printf(" --help \t\t\tShow this help message\n\n"); |
|
|
|
printf("Examples:\n"); |
|
printf(" typeconvert --bin 010100110\n"); |
|
printf(" typeconvert --oct 14270\n"); |
|
printf(" typeconvert --dec 9011\n"); |
|
printf(" typeconvert --hex 0x1234a\n"); |
|
printf(" typeconvert --hex 0x1234A\n"); |
|
printf(" typeconvert --hex 1234a\n"); |
|
printf(" typeconvert --hex 1234A\n"); |
|
printf(" typeconvert --bin 0110 --oct 71 --dec 19 --hex 0xAf01\n\n"); |
|
|
|
printf("https://gist.github.com/IceCereal/11016076686ae6e5a544d058518b1871" |
|
"\n"); |
|
return; |
|
} |
|
|
|
int bin2dec(char *input) |
|
{ |
|
int value = 0; |
|
int input_length = strlen(input) - 1; |
|
|
|
for (int i = input_length; i >= 0; --i){ |
|
if (input[i] == '0') |
|
continue; |
|
else if (input[i] == '1') |
|
value += 1 << input_length - i; |
|
else |
|
assert(0 && "input contained a character other than 0 or 1"); |
|
} |
|
|
|
return value; |
|
} |
|
|
|
void dec2bin(int input, char *output) |
|
{ |
|
int output_length = strlen(output); |
|
int index = 0, first_1_index = -1; |
|
|
|
while (index != output_length) { |
|
if (input & 1 << (output_length - index - 1)) |
|
output[index] = '1'; |
|
else |
|
output[index] = '0'; |
|
++index; |
|
} |
|
} |
|
|
|
int main(int argc, char *argv[]) |
|
{ |
|
static struct option long_options[] = { |
|
{ "bin", required_argument, 0, BIN }, |
|
{ "oct", required_argument, 0, OCT }, |
|
{ "dec", required_argument, 0, DEC }, |
|
{ "hex", required_argument, 0, HEX }, |
|
{ "help" , no_argument , 0, HELP }, |
|
{ 0 , 0 , 0, 0 } |
|
}; |
|
|
|
int first_input = 1; |
|
|
|
while (1){ |
|
int c = getopt_long(argc, argv, "", long_options, NULL); |
|
if (c == -1) |
|
break; |
|
|
|
if (first_input) |
|
first_input = 0; |
|
else |
|
printf("===========================================\n"); |
|
|
|
int decimal_value; |
|
char output[sizeof(int) * 8]; |
|
memset(output, '0', sizeof(int) * 8); |
|
|
|
switch (c){ |
|
case BIN: |
|
decimal_value = bin2dec(optarg); |
|
dec2bin(decimal_value, output); |
|
|
|
printf("Binary: "); |
|
for (int i = 0; i < sizeof(int); ++i) |
|
printf("%.8s ", output + i*8); |
|
printf("\n"); |
|
printf("Octal: %o \n", decimal_value); |
|
printf("Decimal: %d \n", decimal_value); |
|
printf("Hexadecimal: 0x%X \n", decimal_value); |
|
|
|
break; |
|
|
|
case OCT: |
|
sscanf(optarg, "%o", &decimal_value); |
|
|
|
dec2bin(decimal_value, output); |
|
|
|
printf("Binary: "); |
|
for (int i = 0; i < sizeof(int); ++i) |
|
printf("%.8s ", output + i*8); |
|
printf("\n"); |
|
|
|
printf("Octal: %o \n", decimal_value); |
|
printf("Decimal: %d \n", decimal_value); |
|
printf("Hexadecimal: 0x%X \n", decimal_value); |
|
break; |
|
|
|
case DEC: |
|
sscanf(optarg, "%d", &decimal_value); |
|
|
|
dec2bin(decimal_value, output); |
|
|
|
printf("Binary: "); |
|
for (int i = 0; i < sizeof(int); ++i) |
|
printf("%.8s ", output + i*8); |
|
printf("\n"); |
|
|
|
printf("Octal: %o \n", decimal_value); |
|
printf("Decimal: %d \n", decimal_value); |
|
printf("Hexadecimal: 0x%X \n", decimal_value); |
|
break; |
|
|
|
case HEX: |
|
sscanf(optarg, "%x", &decimal_value); |
|
|
|
dec2bin(decimal_value, output); |
|
|
|
printf("Binary: "); |
|
for (int i = 0; i < sizeof(int); ++i) |
|
printf("%.8s ", output + i*8); |
|
printf("\n"); |
|
|
|
printf("Octal: %o \n", decimal_value); |
|
printf("Decimal: %d \n", decimal_value); |
|
printf("Hexadecimal: 0x%X \n", decimal_value); |
|
break; |
|
|
|
case HELP: |
|
print_help(0); |
|
break; |
|
|
|
default: |
|
print_help(1); |
|
break; |
|
} |
|
|
|
} |
|
|
|
return 0; |
|
} |