Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This question has been asked before but the responses have been wrong. Please do not copy and paste their answers. Currently the program opens a

This question has been asked before but the responses have been wrong. Please do not copy and paste their answers. Currently the program opens a file and reads every byte in the file and write both the ASCII hex value for that byte as well as its printable character to standard output with non-printable characters printing a "." Now, I want have an option where the program prints in binary instead of hex by typing "-b" at the command line. For instance, ~hexdump -b input.txt(or any name of text file) will result in the binary option and ~hexdump input.txt results in the hex option. Any ideas how to implement this?

This code is done in C.

----------------------------------

#include #include #include

#define SIZE 1000

void hexDump(void * addr, int len) { int i; unsigned char buffline[17]; unsigned char * pc = (unsigned char * ) addr; for (i = 0; i < len; i++) { if ((i % 16) == 0) { if (i != 0) printf(" %s ", buffline); if (pc[i] == 0x00) { return; //exit will end the program and you cant modify next line } printf(" %07x: ", i); //prints address } printf("%02x", pc[i]); if ((i % 2) == 1) printf(" "); if ((pc[i] < 0x20) || (pc[i] > 0x7e)) { //non-printable chars buffline[i % 16] = '.'; } else { buffline[i % 16] = pc[i]; } buffline[(i % 16) + 1] = '\0'; //clear array } while ((i % 16) != 0) { //'.' fill last line printf(" "); i++; } printf(" %s ", buffline); }

int main(int argc, char * argv[]) { FILE * fp; char buff[SIZE]; int count=1; fp = fopen(argv[1], "r"); memset(buff, '\0', sizeof(buff)); while(fgets(buff, SIZE, fp)){ hexDump(& buff, sizeof(buff)); } fclose(fp);; return 0;

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Systems

Authors: Ramez Elmasri, Shamkant B. Navathe

7th Edition Global Edition

1292097612, 978-1292097619

More Books

Students also viewed these Databases questions