#include <cctype>
#include <iostream>

namespace {
  int to_int(int c) {
    if (not isxdigit(c)) return -1; // error: non-hexadecimal digit found
    if (isdigit(c)) return c - '0';
    if (isupper(c)) c = tolower(c);
    return c - 'a' + 10;
  }

  template<class InputIterator, class OutputIterator> int
  unhexlify(InputIterator first, InputIterator last, OutputIterator ascii) {
    while (first != last) {
      int top = to_int(*first++);
      int bot = to_int(*first++);
      if (top == -1 or bot == -1)
	return -1; // error
      *ascii++ = (top << 4) + bot;
    }
    return 0;
  }
}

int main() {
  char hex[] = "7B5a7D";
  size_t len = sizeof(hex) - 1; // strlen
  char ascii[len/2+1];
  ascii[len/2] = '\0';

  if (unhexlify(hex, hex+len, ascii) < 0) return 1; // error
  std::cout << hex << " -> " << ascii << std::endl;
}
