Skip to content

Instantly share code, notes, and snippets.

@kira924age
Last active November 22, 2022 15:32
Show Gist options
  • Save kira924age/06052bab1deacdb72cb6 to your computer and use it in GitHub Desktop.
Save kira924age/06052bab1deacdb72cb6 to your computer and use it in GitHub Desktop.
Hexdump and Reverse Hexdump by C and Python.
#include <stdio.h>
int main(int argc, char *argv[])
{
int n;
unsigned long long int count = 0;
FILE *fp;
if(argc != 2){
puts("Usage: hex [file_name]");
return 0;
}
if((fp = fopen(argv[1], "rb")) == NULL)
printf("Error: %s is not found.\n", argv[1]);
else{
while((n = fgetc(fp)) >= 0){
printf("%02x ", n);
count++;
if(count%16 == 0)
putchar('\n');
}
fclose(fp);
}
return 0;
}
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import sys
argv = sys.argv
argc = len(argv)
if argc != 2:
print 'Usage: python hex.py [file_name]'
quit()
count = 0
try:
f = open(argv[1], 'rb')
except:
print 'Error: %s is not found.' % argv[1]
quit()
for char in f.read():
print '%02x' % ord(char),
count += 1
if count % 16 == 0:
print
f.close()
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int n;
char str[255];
FILE *fp,*fp2;
if(argc != 2){
puts("Usage: unhex [file_name]");
return 0;
}
if((fp = fopen(argv[1], "rb")) == NULL){
printf("Error: %s Not Available.\n", argv[1]);
return 0;
}
printf("out file name:");
scanf("%254s%*[^\n]", str);
if((fp2 = fopen(str, "wb")) == NULL){
printf("Error: %s Not Available.\n", str);
return 0;
}
while(fscanf(fp, "%x", &n) != EOF)
fputc(n, fp2);
fclose(fp);
fclose(fp2);
return 0;
}
#!/usr/bin/env python
#-*- coding: utf:8 -*-
import sys
argv = sys.argv
argc = len(argv)
if argc != 2:
print 'Usage: python unhex.py [file_name]'
quit()
try:
infile = open(argv[1], 'rb')
except:
print 'Error: %s is not found.' % argv[1]
quit()
outfile = open(raw_input('out file name:'), 'wb')
a = [str(x) for x in infile.read().split()]
for i in a:
outfile.write(chr(int(i, 16)))
infile.close()
outfile.close()
#!/usr/bin/env python
#-*- coding: utf:8 -*-
import sys
argv = sys.argv
argc = len(argv)
if argc != 2:
print 'Usage: python unhex.py [file_name]'
quit()
try:
infile = open(argv[1], 'rb')
data = infile.read()
except:
print 'Error: %s is not found.' % argv[1]
quit()
outfile = open(raw_input('out file name:'), 'wb')
i=0
while i+1 < len(data):
outfile.write(chr(int(data[i:i+2],16)))
i+=2
infile.close()
outfile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment