$hexhex to RGB dump code

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
$hexhex to RGB dump code
by on (#93375)
Some of you might find this useful. Read the example C code in a libpng source tarball to convert to png. You can also do things like gif2ascii etc.

---------8<---- start of C file hex2image.c -------->8---

/* compile with 'gcc -c hex2image.c -o hex2image.o; gcc -o hex2image hex2image.o' */

#include <stdio.h>

int main(int argc, char *argv[], char **envp)
{

if (argc < 2) {/* This returns if no argument is provided */
fprintf (stderr, "Usage : hex2image <filename>\n");
return -1;
}

FILE *fp = fopen (argv[1], "r");
unsigned int i = 0;
int n;
for (; (n = fscanf(fp, "$&x", &i)) != EOF;) {
if (n != 0) {
unsigned long int ui = (unsigned long int) i;
fprintf(stdout, "%lu", ui); /* prints RGB value; */
}
}

return 0;

}

-----8<--------end of C file hex2image.c----------->8-------