#!/usr/bin/perl -w use English; if (@ARGV < 1) { printf "$PROGRAM_NAME filename \n"; printf "Display binary files for Micro I lab. \n"; printf "File format types read:\n"; printf "\n"; printf "format 0: byte data (1 record = 1 byte)\n"; printf "format 1: 16 bit integers, little endian (1 record = 2 bytes)\n"; printf "format 2: 16 bit integers, big endian (1 record = 2 bytes)\n"; printf "format 3: 32 bit integers, little endian (1 record = 4 bytes)\n"; printf "format 4: 32 bit integers, big endian (1 record = 4 bytes)\n"; printf "\n"; printf "For format #0, only one 'data' is allowed, and is a quoted string\n"; printf "\n"; printf "File format:\n"; printf "\n"; printf "byte 0: format number that determines file format\n"; printf "bytes 1,2: # of records (16-bit number , little endian)\n"; printf "bytes: 3 to end -- records\n"; printf "\n"; exit(0); } $fname = $ARGV[0]; printf "Fname: $fname \n"; open(INPUT,"$fname") || die "Can't open input file $fname"; binmode(INPUT); read(INPUT,$fmt,1); read(INPUT,$rlen,2); $fmt_type = unpack("C",$fmt); $num_records = unpack("v",$rlen); printf ("Format type: %d,",$fmt_type); printf (" Record length: %d \n",$num_records); print("Data is : \n"); if ($fmt_type == 0) { for ($i=0;$i<$num_records;$i++){ read(INPUT,$buf,1); $d = unpack("C",$buf); printf ("%02x ('%c')\n",$d,$d); } }elsif ($fmt_type == 1) { for ($i=0;$i<$num_records;$i++){ read(INPUT,$buf,2); $d = unpack("v",$buf); printf ("%04x ('%d')\n",$d,$d); } }elsif ($fmt_type == 2) { for ($i=0;$i<$num_records;$i++){ read(INPUT,$buf,2); $d = unpack("n",$buf); printf ("%04x ('%d')\n",$d,$d); } }elsif ($fmt_type == 3) { for ($i=0;$i<$num_records;$i++){ read(INPUT,$buf,4); $d = unpack("V",$buf); printf ("%08x ('%d')\n",$d,$d); } }elsif ($fmt_type == 4) { for ($i=0;$i<$num_records;$i++){ read(INPUT,$buf,4); $d = unpack("N",$buf); printf ("%08x ('%d')\n",$d,$d); } } else { printf "Unsupported format type: $fmt_type, exiting....\n"; exit(0); } close(OUTPUT);