Cody.L
Cody.L

Reputation: 31

convert binary file to hexadecimal characters

How do I convert the contents of a binary file into hexadecimal?

ex. test.bin contains

abcdefghijklmn in binary 

Convert contents to output.txt which will contain

61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e

Upvotes: 1

Views: 4648

Answers (4)

netskink
netskink

Reputation: 4539

You can literally, use octal dump command to convert the string. See here:

$ echo abcdefg | od -t x1 -An
 61 62 63 64 65 66 67 0a

Upvotes: 0

Luis Colorado
Luis Colorado

Reputation: 12635

This program will do (i think) what you want:

#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 */

Sorry, but it's commented in spanish :} It's an old program i wrote.

Upvotes: 0

ryanpattison
ryanpattison

Reputation: 6251

To print a value in hexadecimal format you can use "%x" format specifier.

Here is a little program that prints the the contents in hex of files given as command line arguments. To print to an output file, use 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;
}

Upvotes: 4

Marcin Kolny
Marcin Kolny

Reputation: 633

Application which converts string from file to hexadecimal numbers and saves it to another file:

#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;
}

Upvotes: 0

Related Questions