Glenn Butcher

Home
About Us
foo
incexp
Journal Archive
onedrive
Photographs
Picture Albums
Slashdot Quotes
slideshow
Weather
Resume (.pdf, 17K)
Pictures

Transitory

Teaching

Research
Dissertation (.pdf, 446K)

News
CNN: Technology
CNN: Top Stories
COS Gazette: Headlines
COS Gazette: Metro
CSM
Honolulu Advertiser
Internet News
LWN
Newsforge
NPR News
NY Times
PCMag: Reviews
Slashdot
Space.com
The Hawaii Channel
Toms Hardware: Articles
Toms Hardware: News
Washington Post
Yahoo: Boeing
Yahoo: Missile Defense
Yahoo: Tech Tuesday
Yahoo: Top Stories

Software
HexDump
Home DNS
InOut Board
LOCOMGR
RRController
ttygcc
UBW IR Receiver
Walkabout
website
Gantt Chart
Trainsheet
Google Map Test

Railroading
LaBelle Long Caboose
NG TerraServers
Cumbres and Toltec Maps

Other Stuff
Gumbo Recipe
Links
Salsa Recipe
Weather Station
Website Tags

hexdump.c: So You Don't Have To...

I needed a binary file viewer, and the downloadable alternatives have too many features. So, I wrote one in portable C; I've compiled it for Windows using WxDevC++, and on my Debian Linux server. Enjoy...


#include <stdio.h>
#include <stdlib.h>

void byte2hex(char numb, char *hex) { char hexchars[17] = "0123456789ABCDEF"; hex[0] = hexchars[(numb >> 4) & 0xF]; hex[1] = hexchars[numb & 0xF]; hex[2] = 0x00; }
void bufclean (char *buf, int len) { int i; for (i=0; i<len; i++) { if (buf[i] < '\x20' | buf[i] > '\x7e') buf[i] = '.'; } }
int main(int argc, char *argv[]) { FILE *fp; int num, tot, i; tot = 0; char buf[24]; char str[5]; if ((fp=fopen(argv[1], "rb")) == NULL) exit(EXIT_FAILURE); while ((num = fread(buf,1,16,fp)) > 0 ) { printf("%8d: ",tot); for (i=0; i<16; i++) { if (i>=num) { printf(" "); } else { byte2hex(buf[i], str); printf("%02s ",str); } if (i==7) printf(" "); } bufclean(buf,num); printf(" ["); for (i=0; i<num; i++) { printf("%1c",buf[i]); } if (num < 16) for (i=0; i<16-num; i++) printf(" "); printf("]\n"); tot += num; } fclose(fp); printf("end: %d bytes\n", tot); return EXIT_SUCCESS; }