Kjetil's Information Center: A Blog About My Projects

Number Conversion Tool

Here is another homemade tool that I use when dealing with numbers during programming, analysis, etc. The program will simply output its argument in decimal, hex and binary notation. The argument can be specified in any of the three notations by prefixing the number with 0x or 0b, or no prefix for decimal numbers. There is also support for specifying addition (+), subtraction (-) or xor (^), and then a second number to perform this operation.

The code is loosely based on the binary dumping tool presented earlier, and can be found here:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static int power_of_two(int n)
{
  if (n == 0)
    return 1;
  else
    return 2 * power_of_two(n - 1);
}

static char *convert_to_bin(int integer)
{
  static char buffer[33]; /* Should not be longer than 32-bits + NULL. */
  int i, first_one;

  buffer[32] = '\0'; /* Always terminate. */

  first_one = 31;
  for (i = 31; i >= 0; i--) {
    if (integer - power_of_two(i) >= 0) {
      buffer[31 - i] = '1';
      if (first_one > (31 - i))
        first_one = (31 - i);
      integer -= power_of_two(i);
    } else
      buffer[31 - i] = '0';
  }

  return &buffer[first_one];
}

static int convert_from_bin(char *number)
{
  int value, n;
  char *p;

  value = 0; 
  n = strlen(number) - 3; /* Also minus "0b". */
  p = &number[2];
  do {
    if (*p == '0') {
      n--;
    } else if (*p == '1') {
      value += power_of_two(n);
      n--;
    }
  } while (*p++ != '\0');

  return value;
}

static int convert_to_int(char *number)
{
  int integer;

  if (strncmp(number, "0x", 2) == 0) {
    sscanf(number, "0x%x", &integer);
  } else if (strncmp(number, "0b", 2) == 0) { 
    integer = convert_from_bin(number);
  } else {
    integer = atoi(number);
  }
  
  return integer;
}

static void display_int(int integer, char *prefix)
{
  printf("%s%-10u 0x%-10x 0b%s\n",
    prefix, integer, integer, convert_to_bin(integer));
}

int main(int argc, char *argv[])
{
  int n1, n2, result;
  char *prefix;

  if (argc == 2) { /* Just display the number in different forms. */
    n1 = convert_to_int(argv[1]);
    display_int(n1, "   ");

  } else if (argc == 4) { /* Perform extra operation. */
    n1 = convert_to_int(argv[1]);
    n2 = convert_to_int(argv[3]);
    if (argv[2][0] == '+') {
      result = n1 + n2;
      prefix = " + ";
    } else if (argv[2][0] == '-') {
      result = n1 - n2;
      prefix = " - ";
    } else if (argv[2][0] == '^') {
      result = n1 ^ n2;
      prefix = " ^ ";
    } else {
      fprintf(stderr, "%s: error: invalid operator.\n", argv[0]);
      return -1;
    }
    display_int(n1, "   ");
    display_int(n2, prefix);
    display_int(result, " = ");

  } else {
    fprintf(stderr, "Usage: %s <number> [<+|-|^> <number>]\n", argv[0]);
    fprintf(stderr, "  number can be decimal, hex (0x) or binary (0b)\n");
    return -1;
  }
   
  return 0;
}
          


Topic: Scripts and Code, by Kjetil @ 06/04-2009, Article Link