A hex compiler in C

Here is a sample of a programming language I call “Hex”:

# hello_world.hex
# compiles to "hello_world.txt"

48 65 6c 6c 6f  # "Hello"
20              # space
77 6f 72 6c 64  # "world"
0a              # newline

In Hex, you can write not just programs, but videos, images, text files, anything. You write programs in Hex by writing the hexadecimal codes for each byte of, say, an ELF binary for Linux on Intel x86. These programs are completely readable, because you can also write comments.

To run a Hex program, you first pass it to the hex compiler:

$ hex < hello_world.hex > hello_world.txt
$ cat hello_world.txt
Hello world

Here is a compiler for Hex, written in C:

#include <stdio.h>
int to_hex(char c) {
  if      ('0' <= c && c <= '9') { return      c-'0'; }
  else if ('a' <= c && c <= 'f') { return 10 + c-'a'; }
  return -1;
}
int main(void) {
  for (;;) {
    int c = getchar();
    if (to_hex(c) != -1) {
      int c2 = getchar();
      if (to_hex(c2) != -1) { putchar(0x10*to_hex(c) + to_hex(c2)); }
      else { return 1; }
    }
    else if (c == EOF) { break; }
    else if (c == '#') { while (getchar() != '\n'); }
    else if (c == ' ' || c == '\n') { continue; }
    else { return 1; }
  }
  return 0;
}
I just released Vidrio, a free app for macOS and Windows to make your screen-sharing awesomely holographic. Vidrio shows your webcam video on your screen, just like a mirror. Then you just share or record your screen with Zoom, QuickTime, or any other app. Vidrio makes your presentations effortlessly engaging, showing your gestures, gazes, and expressions. #1 on Product Hunt. Available for macOS and Windows.

With Vidrio

With generic competitor

More by Jim

Tagged . All content copyright James Fisher 2018. This post is not associated with my employer. Found an error? Edit this page.