如何将二进制文件的内容转换为十六进制?
离。 test.bin包含
abcdefghijklmn in binary
将内容转换为包含
的output.txt61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e
答案 0 :(得分:3)
要以十六进制格式打印值,您可以使用"%x"
format specifier。
这是一个小程序,以十六进制的形式打印作为命令行参数给出的文件。要打印到输出文件,请使用fprintf
#include <stdio.h>
int main(int argc, const char *argv[]) {
int i;
for (i = 1; i < argc; ++i) {
FILE* fp = fopen(argv[i], "rb");
if (fp) { /* ignore if failed to open */
int c;
while ((c = fgetc(fp)) != EOF) {
printf(" %02x", c);
}
fclose(fp);
}
}
return 0;
}
答案 1 :(得分:0)
将字符串从文件转换为十六进制数字并将其保存到另一个文件的应用程序:
#include <stdio.h>
#include <string.h>
char tonum(char c)
{
return c + (c < 10 ? 48 : 87);
}
void to_hex(int input, char* output)
{
int p = input / 16;
output[0] = tonum(p);
output[1] = tonum(input - p*16);
}
int main(void)
{
FILE *input = fopen("input", "r");
FILE *output = fopen("output", "w");
int x;
char out[4] = {0}; out[2] = ' ';
while ((x = fgetc(input)) != EOF)
{
to_hex(x, out);
fputs(out, output);
}
fclose(input);
fclose(output);
return 0;
}
答案 2 :(得分:0)
这个程序(我认为)会做你想要的:
#include <sys/types.h>
#include <signal.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FALSE 0
#define TRUE (!FALSE)
#define TAM_BUFFER 16
char *nomprog; /* nombre del programa */
void error (int, char *, ...);
void signal_handler (int);
main (int argc, char **argv)
{
FILE *fp;
/* valor booleano para ver si se procesa stdin */
int procesar_stdin;
int res;
/* Instalamos el tratamiento de interrupciones */
signal (SIGHUP, signal_handler);
signal (SIGINT, signal_handler);
signal (SIGTERM, signal_handler);
procesar_stdin = TRUE;
res = 0;
for (nomprog = *argv, argc--, argv++; argc > 0; argc--, argv++) {
procesar_stdin = FALSE;
fp = fopen (*argv, "r");
if (fp == NULL) {
error (0, "Error al abrir \"%s\"", *argv);
res = 1;
continue;
}
printf("[%s]:\n", *argv);
procesa (fp);
}
if (procesar_stdin) procesa (stdin);
exit (res);
} /* main */
volatile int interrumpido = FALSE;
void signal_handler (int dumb)
{
interrumpido = TRUE;
}
procesa (FILE *fp)
{
unsigned char buffer [TAM_BUFFER];
unsigned char bufferref [TAM_BUFFER];
unsigned long offset;
int leidos,
asterisco;
for ( offset = 0L;
(!interrumpido) &&
((leidos = fread (buffer,
sizeof (unsigned char),
TAM_BUFFER,
fp)) > 0);
offset += leidos)
{
int j;
if ( offset != 0 &&
memcmp (buffer, bufferref, TAM_BUFFER) == 0)
{
if (!asterisco) printf ("*\n");
asterisco = 1;
continue;
}
asterisco = 0;
memcpy (bufferref, buffer, TAM_BUFFER);
printf ("%08lx : ", offset);
for (j = 0; j < TAM_BUFFER; j++)
if (j < leidos)
printf ("%02x ", buffer [j]);
else printf (" ");
printf (": ");
for (j = 0; j < TAM_BUFFER; j++)
if (j < leidos)
if (isprint (buffer [j]))
printf ("%c", buffer [j]);
else printf (".");
else break;
printf ("\n");
} /* for */
printf ("%08x\n", offset);
} /* procesa */
void error (int n, char *cad, ...)
{
va_list p;
char mensaje [1000];
va_start (p, cad);
vsprintf (mensaje, cad, p);
va_end (p);
if (n) {
fprintf (stderr,
"\n%s: \007ERROR: %s\n",
nomprog,
mensaje);
exit (n);
} else fprintf (stderr,
"\n%s: \007WARNING: %s\n",
nomprog,
mensaje);
} /* error */
很抱歉,但是请用西班牙语发表评论:}这是我写的旧程序。