hextools

hextools repository
git clone git://mcdim.xyz/hextools.git
Log | Files | Refs | README

unhex.c (489B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 #include <inttypes.h>
      5 
      6 #define BUFFER_SIZE 128
      7 
      8 static uintmax_t
      9 hex2int(char *str)
     10 {
     11 	uintmax_t n;
     12 	sscanf(str, "%"SCNxMAX, &n);
     13 	return n;
     14 }
     15 
     16 int
     17 main(int argc, char **argv)
     18 {
     19 	if (argc == 2) {
     20 		uintmax_t n = hex2int(argv[1]);
     21 		printf("%"PRIuMAX"\n", n);
     22 
     23 		return 0;
     24 	}
     25 
     26 	char buffer[BUFFER_SIZE];
     27 	while (fgets(buffer, sizeof(buffer), stdin)) {
     28 		uintmax_t n = hex2int(buffer);
     29 		printf("%"PRIuMAX"\n", n);
     30 	}
     31 
     32 	return 0;
     33 }