Skip to content

Instantly share code, notes, and snippets.

@aessam
Created November 10, 2014 18:50
Show Gist options
  • Select an option

  • Save aessam/14109f75f655076eceda to your computer and use it in GitHub Desktop.

Select an option

Save aessam/14109f75f655076eceda to your computer and use it in GitHub Desktop.
Simple C application that reads file and print it as HEX,
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int lineBreaker = 12;
void printInHex(char* file, int length){
char* temp = (char*)malloc(lineBreaker+1);
memset(temp,0,lineBreaker+1);
for (int i=0;i<length;i++){
printf("0x%02x ",file[i]);
if(file[i]>='0')
temp[(i%lineBreaker)-1] = file[i];
else
temp[(i%lineBreaker)-1] = '*';
if(i%lineBreaker==0)
printf("%s%c \n",temp,file[i]);
}
printf("\n");
}
int main(int argc,char** argv){
if(argc>=2){
if(argc==3) lineBreaker = atoi(argv[2]);
FILE* target = fopen(argv[1],"rb");
if (target)
{
fseek (target, 0, SEEK_END);
int length = ftell (target);
fseek (target, 0, SEEK_SET);
char* buffer = (char*)malloc (length);
if (buffer)
{
fread (buffer, 1, length, target);
}
fclose (target);
printInHex(buffer, length);
free(buffer);
}else{
printf("Can't read the file.");
}
}else{
printf("Usage: hex filename.\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment