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

/* utility calls */
unsigned char readHexByte(const char *text, unsigned char *result) {
  unsigned char tempa,tempb;
  if (text[0] == '\0') return 0; /* return no error if no arg supplied */
  tempa = text[0] - 0x30;
  if (tempa > 9) tempa -= 7; // A..F
  if (tempa > 15) tempa -= 0x20; // a..f
  if (tempa > 15) return -1; // invalid conversion
  tempb = text[1] - 0x30;
  if (tempb > 9) tempb -= 7;
  if (tempb > 15) tempb -= 0x20; 
  if (tempb > 15) return -1; // invalid conversion
  *result = (tempa << 4) + tempb;
  return 0;
}

unsigned char readHexWord(const char *text, signed short *result) {
  unsigned char tempa, tempb;
  if (text[0] == '\0') return 0; /* return no error if no arg supplied */
  if (readHexByte(text,&tempa)) return -1;
  if (readHexByte(text+2,&tempb)) return -1;
  *result = tempa * 256 + tempb;
  return 0;
}

int main() {
  char text[] = "FF1D";
  signed short fred;
  readHexWord(text,&fred);
  printf("%d\n",fred);
}
