Haber, no sabía como poner esto
hexactamente, así que pense en un nombre:
Hexadecimal Dumper And To C Convertor
Creo que no hace falta explicar mucho, es un dumpeador hexadecimal para convertir cualquier archivo a variable del tipo char en C. En otras palabras, convierte:
#include <stdio.h>
double factorial(short int numero) {
int pred = 0;
double tmp = numero;
for (pred = --numero; pred > 1; --pred)
tmp *= pred;
return tmp;
}
int main(void) {
double factor87 = factorial(87);
printf("Factorial de 87: %.20lg", factor87
); return 0;
}
En esto:
char hexdump2c[] = {
0x23, 0x69, 0x6e, 0x63, 0x6c, 0x75, 0x64, 0x65, 0x20, 0x3c, 0x73, 0x74, 0x64, 0x69, 0x6f, 0x2e, 0x68, 0x3e,
0x0d, 0x0a, 0x0d, 0x0a, 0x64, 0x6f, 0x75, 0x62, 0x6c, 0x65, 0x20, 0x66, 0x61, 0x63, 0x74, 0x6f, 0x72, 0x69,
0x61, 0x6c, 0x28, 0x73, 0x68, 0x6f, 0x72, 0x74, 0x20, 0x69, 0x6e, 0x74, 0x20, 0x6e, 0x75, 0x6d, 0x65, 0x72,
0x6f, 0x29, 0x20, 0x7b, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x69, 0x6e, 0x74, 0x20, 0x70, 0x72, 0x65, 0x64,
0x20, 0x3d, 0x20, 0x30, 0x3b, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x64, 0x6f, 0x75, 0x62, 0x6c, 0x65, 0x20,
0x74, 0x6d, 0x70, 0x20, 0x3d, 0x20, 0x6e, 0x75, 0x6d, 0x65, 0x72, 0x6f, 0x3b, 0x0d, 0x0a, 0x20, 0x20, 0x20,
0x20, 0x66, 0x6f, 0x72, 0x20, 0x28, 0x70, 0x72, 0x65, 0x64, 0x20, 0x3d, 0x20, 0x2d, 0x2d, 0x6e, 0x75, 0x6d,
0x65, 0x72, 0x6f, 0x3b, 0x20, 0x70, 0x72, 0x65, 0x64, 0x20, 0x3e, 0x20, 0x31, 0x3b, 0x20, 0x2d, 0x2d, 0x70,
0x72, 0x65, 0x64, 0x29, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x74, 0x6d, 0x70, 0x20,
0x2a, 0x3d, 0x20, 0x70, 0x72, 0x65, 0x64, 0x3b, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x72, 0x65, 0x74, 0x75,
0x72, 0x6e, 0x20, 0x74, 0x6d, 0x70, 0x3b, 0x0d, 0x0a, 0x7d, 0x0d, 0x0a, 0x0d, 0x0a, 0x69, 0x6e, 0x74, 0x20,
0x6d, 0x61, 0x69, 0x6e, 0x28, 0x76, 0x6f, 0x69, 0x64, 0x29, 0x20, 0x7b, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20,
0x64, 0x6f, 0x75, 0x62, 0x6c, 0x65, 0x20, 0x66, 0x61, 0x63, 0x74, 0x6f, 0x72, 0x38, 0x37, 0x20, 0x3d, 0x20,
0x66, 0x61, 0x63, 0x74, 0x6f, 0x72, 0x69, 0x61, 0x6c, 0x28, 0x38, 0x37, 0x29, 0x3b, 0x0d, 0x0a, 0x20, 0x20,
0x20, 0x20, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x66, 0x28, 0x22, 0x46, 0x61, 0x63, 0x74, 0x6f, 0x72, 0x69, 0x61,
0x6c, 0x20, 0x64, 0x65, 0x20, 0x38, 0x37, 0x3a, 0x20, 0x25, 0x2e, 0x32, 0x30, 0x6c, 0x67, 0x22, 0x2c, 0x20,
0x66, 0x61, 0x63, 0x74, 0x6f, 0x72, 0x38, 0x37, 0x29, 0x3b, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x67, 0x65,
0x74, 0x63, 0x68, 0x61, 0x72, 0x28, 0x29, 0x3b, 0x20, 0x20, 0x20, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x72,
0x65, 0x74, 0x75, 0x72, 0x6e, 0x20, 0x30, 0x3b, 0x0d, 0x0a, 0x7d, 0x0d, 0x0a};
El código fuente del programa es:
/* Simple dumpeador de archivos a hexadecimal y guardar en C
* http://mikygonzalez.16mb.com
*/
#include <stdio.h>
#define TAM_HEXCODE 18
/*! Función principal hexdump */
void hexdump(FILE *archivo_fd) {
unsigned short int bucle_i;
char caracter_actual = '\0';
fseek(archivo_fd
, 0, SEEK_SET
);
while(!feof(archivo_fd
)) { for(bucle_i = 0; bucle_i < TAM_HEXCODE; bucle_i++) {
caracter_actual
= fgetc(archivo_fd
); if(caracter_actual == EOF)
return (void) 0;
printf("0x%.2x, ", caracter_actual
); }
}
}
int main(int argc, char *argv[]) {
FILE *archivo_fd;
if(argc < 2)
return 1;
if(!(archivo_fd
= fopen(argv
[1], "rb"))) return 1;
printf("char hexdump2c[] = {\n"); hexdump(archivo_fd);
return 0;
}
También pueden encontrarse este y otros programas en:
http://pastes.mikygonzalez.16mb.com/?ad2cea935c65385a#c0bI9LQ9BD6f8snTLpklw/LgzXRnOEs6lJHJWqbbb9c= o en mi pagina personal (en la firma).
Saludos