Skip to content

Instantly share code, notes, and snippets.

@IceCereal
Last active April 3, 2023 12:57
Show Gist options
  • Save IceCereal/11016076686ae6e5a544d058518b1871 to your computer and use it in GitHub Desktop.
Save IceCereal/11016076686ae6e5a544d058518b1871 to your computer and use it in GitHub Desktop.
A command line utility to convert a number of binary, octal, decimal or hexadecimal to the other three
files = typeconvert.c typeconvert.h
typeconvert: $(files)
cc -o typeconvert $(files)
.PHONY: clean
clean:
rm typeconvert
#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;
}
#ifndef H_TYPECONVERT_H
#define H_TYPECONVERT_H
#define BIN 2
#define OCT 8
#define DEC 10
#define HEX 16
#define HELP 20
void print_help(int);
int bin2dec(char *input);
int oct2dec(char *input);
int hex2dec(char *input);
void dec2bin(int input, char *output);
#endif

Use Typeconvert_V2.

Typeconvert

Why can't I find a single and simple command line utility to convert a number in one base to another base.

Yeah, it's simple to make your own program or launch a python shell to get it to print a number out in a different base or go to rapidtables.com, but why can't I find a single command line utility to do this for me. Either way, enough ranting. Here's some code.

This isn't important enough to warrant a repository and that's why it's a gist of 4 files.

Precautions:

  • This assumes little endian representation.
  • This has been only programmed for int (signed) because I didn't have the need to make it long so, ymmv on your system. It will not work for numbers that go out of the range of int.
  • If you want to modify it in anyway, go right ahead. This is "licensed" with the do-whatever-the-heck-you-want-with-it-but-im-not-responsible-for-anything license

Compile

Run make and then move the object (typeconvert) to your /path/to/bin folder to use it as a system wide utility.

Examples

typeconvert --help

typeconvert --bin 010100110

typeconvert --oct 14270

typeconvert --dec 9011

typeconvert --hex 0x1234a

typeconvert --hex 0x1234A

typeconvert --hex 1234a

typeconvert --hex 1234A

typeconvert --bin 0110 --oct 71 --dec 19 --hex 0xAf01

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment