├── Makefile ├── README.md ├── ascii.c ├── dec.c ├── hex.c ├── libhex.c └── libhex.h /Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | RM = rm -rf 3 | CP = cp 4 | 5 | all: libhex.so 6 | 7 | libhex.o: libhex.c 8 | $(CC) -c -fPIC $< -o $@ 9 | 10 | libhex.so: libhex.o 11 | $(CC) -shared -fPIC $< -o $@ 12 | 13 | install: 14 | $(CP) *.so /usr/lib/ 15 | $(CP) *.h /usr/include/ 16 | 17 | clean: 18 | $(RM) *.o 19 | $(RM) *.so 20 | $(RM) ascii 21 | $(RM) dec 22 | $(RM) hex 23 | 24 | hex: hex.c 25 | $(CC) $< -o $@ -lhex 26 | 27 | dec: dec.c 28 | $(CC) $< -o $@ -lhex 29 | 30 | ascii: ascii.c 31 | $(CC) $< -o $@ -lhex 32 | 33 | test: hex dec ascii 34 | ./hex 2014 1024 512 256 128 64 16 0 35 | ./dec 7DE 400 200 100 80 40 10 0 36 | ./ascii --table 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Installation 2 | ====== 3 | 4 | This will compile and install the files in your /usr/lib and /usr/include directories. 5 | 6 | make 7 | make install 8 | 9 | Make a test with : 10 | 11 | make test 12 | // Will compile and run two test executables 13 | // ./hex 2014 1024 512 256 128 64 16 0 14 | // ./dec 7DE 400 200 100 80 40 10 0 15 | 16 | Clean output files with : 17 | 18 | make clean 19 | 20 | Usage 21 | ====== 22 | 23 | Include the following librairy : 24 | 25 | 26 | 27 | Use the flag -lhex ~~and -lm~~ to link with the libhex librairy ~~and math librairy~~. 28 | 29 | Fonctions 30 | ------ 31 | 32 | ###char* dechex ( unsigned dec, char *hex, int len, int fill ) 33 | 34 | 35 | Convert a Decimal unsigned int in an Hexadecimal string. 36 | First argument is the decimal to convert. 37 | The second one is the output buffer, if NULL passed it will use malloc to allocate sufficient memory, so don't forget to free() if you're programming in C/C++. 38 | The next argument is buffer's length can be any value if NULL is passed because length wil be calculated. 39 | And the last argument is to make string start at first address and filled with zero at end. 40 | 41 | ###void init_hexdec ( void ) 42 | 43 | Intitialize the hexadecimal to decimal conversion array. 44 | 45 | ###unsigned hexdec ( const unsigned char *hex, const int s_hex ) 46 | 47 | Convert an Hexadecimal string to a Decimal int. 48 | 49 | Check for errno == EINVAL, if string contains invalid hexadecimal character. 50 | 51 | Note : Call the function init_hexdec() once before you convert hexadecimal to decimal. 52 | 53 | ###char hexascii ( const unsigned char *hex ) 54 | 55 | Convert two characters length string to character according the ASCII Table. 56 | 57 | Note : Call the function init_hexdec() once before you convert decimal to hexadecimal. 58 | 59 | TODO 60 | ====== 61 | 62 | - Make fonctions for encoding/decoding ASCII strings. 63 | 64 | -------------------------------------------------------------------------------- /ascii.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main (const int argc, const char *argv[]) { 8 | if (argc == 0) { 9 | exit(EXIT_FAILURE); 10 | return -1; 11 | } 12 | 13 | int i; 14 | 15 | init_hexdec(); 16 | 17 | if (strcmp(argv[1], "--table") == 0) { 18 | char *hex = calloc(9, sizeof(char)), *conv; 19 | for (i = 0; i < 127; i++) { 20 | conv = dechex(i, hex, 8, 0); 21 | printf("%d <=> %s <=> %c\r\n", i, conv, hexascii(conv)); 22 | } 23 | free(hex); 24 | 25 | exit(EXIT_SUCCESS); 26 | return 0; 27 | } 28 | 29 | for (i = 1; i < argc; i++) { 30 | if (strlen(argv[i]) > 1) { 31 | printf("%c", hexascii(argv[i])); 32 | } 33 | } 34 | printf("\r\n"); 35 | 36 | exit(EXIT_SUCCESS); 37 | return 0; 38 | }; 39 | 40 | -------------------------------------------------------------------------------- /dec.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main (const int argc, const char *argv[]) { 8 | 9 | if (argc == 1) { 10 | exit(EXIT_FAILURE); 11 | return -1; 12 | } 13 | 14 | init_hexdec(); 15 | 16 | int i; 17 | for (i = 1; i < argc; i++) { 18 | printf("%d\r\n", hexdec(argv[i], strlen(argv[i]))); 19 | } 20 | 21 | exit(EXIT_SUCCESS); 22 | return 0; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /hex.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main (const int argc, const char *argv[]) { 8 | 9 | if (argc == 1) { 10 | exit(EXIT_FAILURE); 11 | return -1; 12 | } 13 | 14 | int i; 15 | time_t now, now2; 16 | char *hex = calloc(9, sizeof(char)), *conv = NULL; 17 | for (i = 1; i < argc; i++) { 18 | time(&now); 19 | conv = dechex(atoi(argv[i]), hex, 8, 0); 20 | time(&now2); 21 | printf("%f\t%s\r\n", difftime(now2, now), conv); 22 | } 23 | free(hex); 24 | 25 | exit(EXIT_SUCCESS); 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /libhex.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | 6 | int mhexdec[256]; 7 | char mdechex[17] = "0123456789ABCDEF"; 8 | 9 | void init_hexdec () { 10 | int i = 0, n = 0; 11 | for (i; i < 256; ++i) { 12 | if ((i > 47 && i < 58) || (i > 64 && i < 71) || (i > 96 && i < 103)) { 13 | mhexdec[i] = n++; 14 | } else { 15 | mhexdec[i] = -1; 16 | if (i == 71) { 17 | n = 10; 18 | } 19 | } 20 | } 21 | }; 22 | 23 | char* dechex (unsigned dec, char *hex, int len, int fill) { 24 | if (hex == NULL) { 25 | len = dec & 0xFFFF0000 ? (dec & 0xFF000000 ? (dec & 0xF0000000 ? 8 : 7) : (dec & 0x00F00000 ? 6 : 5)) : (dec & 0x0000FF00 ? (dec & 0x0000F000 ? 4 : 3) : (dec & 0x000000FF ? (dec & 0x000000F0 ? 2 : 1) : 1)); 26 | hex = malloc((len+1)*sizeof(char)); 27 | } 28 | char *hex2 = hex+len, *end = hex2; 29 | *hex2 = '\0'; 30 | for (--hex2; ; hex2--) { 31 | *hex2 = mdechex[dec & 0xF]; 32 | dec >>= 4; 33 | if (dec == 0) { 34 | break; 35 | } 36 | } 37 | // Make the string start at correct address. 38 | if (fill) { 39 | if (hex2 > hex) { 40 | char *c = hex, *s = hex2; 41 | for (c, s; s < end; s++) { 42 | *(c++) = *s; 43 | } 44 | // Fill with zeros at end 45 | for (c; c < end; c++) { 46 | *(c++) = '\0'; 47 | } 48 | } 49 | return hex; 50 | } 51 | return hex2; 52 | }; 53 | 54 | /** Keeped for utility purpose : Pointer suppressed to limit memory fetches */ 55 | int ishexchar (const int c) { 56 | return ( (c > 47 && c < 58) || (c > 64 && c < 71) || (c > 96 && c < 103) ) ? 1 : 0; 57 | }; 58 | 59 | unsigned hexdec (const unsigned char *hex, const int s_hex) { 60 | if (s_hex == 0) { 61 | return 0; 62 | } 63 | unsigned d = 0, dv = 0, i; 64 | for (i = 0; i < s_hex; i++) { 65 | dv = mhexdec[(int) hex[i]]; 66 | if (dv < 0) { 67 | errno = EINVAL; 68 | return 0; 69 | } 70 | d += dv * (1 << ((s_hex-i-1) << 2)); 71 | } 72 | return d; 73 | }; 74 | 75 | char hexascii (const unsigned char *hex) { 76 | int i1 = mhexdec[(int) hex[0]], i2 = mhexdec[(int) hex[1]]; 77 | if (i1 > -1 && i2 > -1) { 78 | return (char) ( i1*16 + i2 ); 79 | } else { 80 | errno = EINVAL; 81 | return '\0'; 82 | } 83 | }; 84 | 85 | -------------------------------------------------------------------------------- /libhex.h: -------------------------------------------------------------------------------- 1 | #ifndef DEF_LIBHEX 2 | #define DEF_LIBHEX 3 | 4 | // Decimal to Hexadecimal 5 | 6 | char* dechex (int dec, char *hex, int len, int fill); 7 | 8 | // Hexadecimal to Decimal 9 | 10 | void init_hexdec (); 11 | 12 | int ishexchar (const int c); 13 | 14 | //unsigned hex1dec (const char *hex); 15 | 16 | //unsigned hex2dec (const char *hex); 17 | 18 | unsigned hexdec (const unsigned char *hex, const int s_hex); 19 | 20 | char hexascii (const unsigned char *hex); 21 | 22 | #endif 23 | 24 | --------------------------------------------------------------------------------