Question

Write a C program which will display the contents of a file in base-16 (hexadecimal) and...

Write a C program which will display the contents of a file in base-16 (hexadecimal) and in ASCII.

Complete the following tasks:

  1. Obtain the name of the input file from the command line. If the command-line is “./hexdump xxx.bin” then argv[1] will contain “xxx.bin”.
  2. Open the file for binary input
  3. Print the entire file, 16-bytes per line. Each line should begin with an 8-digit hexadecimal offset into the file. This is the count of the bytes that you have already printed, and therefore begins with 00000000. Each of the 16 bytes should be printed as a 2-digit hexadecimal value separated by spaces. Print leading zeros if necessary to ensure 2 digits. After all 16 bytes are printed, display any values which are printable as ASCII characters, and display any non-printable values as ‘.’. Therefore there will be 16 symbols at the end of the line.

Some useful hints:

  1. An integer can be printed in hexadecimal by using the format string “%x”. If you want to fix the width of the printed value, put the width before the x “%8x”. If you want leading zeros to ensure all values have the same width, put a 0 before this: “%08x”. This works for any number of digits.
  2. To determine if a character is printable, include the header <ctype.h> and use the isprint(c) macro. This checks if the character in the variable ‘c’ is printable, and returns true or false.
  3. To print a value as a character, use the “%c” format string.
  4. To print a linefeed, add “\n” to your format string. If your printf does not have this linefeed, then the next printf will append to the current line. This is how you can print several values on a line in a loop.
  5. File I/O can be done using fopen and fread. Look at the man pages for these, “man 3 fopen”, “man 3 fread”
  6. You can check for end-of-file with feof().

Requirements:

  • Your program should be named “hexdump.c” without the quotes.

Sample Output (user input is in bold)

hexdump -C yyy.bin

00000000 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 |.ELF............|
00000010 03 00 03 00 01 00 00 00 f7 32 00 00 34 00 00 00 |.........2..4...|
00000020 94 51 02 00 00 00 00 00

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Given below is the code for the question. Please do rate the answer if it helped. Thank you.


#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
   FILE *fp;
   char data[16];
   int count, i;
   int offset = 0;
  
   if(argc != 2){
       printf("Usage: %s <filename>\n", argv[0]);
       return 1;
   }
  
   fp = fopen(argv[1], "rb");
   if(fp == NULL){
       printf("ERROR: could not open file %s for reading\n", argv[1]);
       return 1;
   }
  
   count = fread(data, sizeof(char), 16 * sizeof(char), fp);
   while(count != 0){
       printf("%08x", offset);
       //print the hex values of bytes
       for(i = 0; i < count; i++)
           printf(" %02x", data[i]);
      
       //print spaces if count is less than 16 ... to align with printable characters
       for(i = count; i < 16; i++)
           printf(" %2c", ' ');
       printf(" ");
       //print the printable character
       for(i = 0 ;i < count; i++){
           if(isprint(data[i]))
               printf("%c", data[i]);
           else
               printf(".");
       }
       offset += count;
       printf("\n");
       count = fread(data, sizeof(char), 16 * sizeof(char), fp);
   }
   fclose(fp);
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
Write a C program which will display the contents of a file in base-16 (hexadecimal) and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please read this comment: This is the only file given by my professor and so can...

    Please read this comment: This is the only file given by my professor and so can you please help me to write a python program. You can use any test file and post the output with the code. The output might be different then above but please help me to get started with the PYTHON code. Thank you in advance You are going to write a Python program that represents a command line version of a hexadecimal editor. This program...

  • Write a program that implements an elementary bit stream cipher. An elementary level bit stream cipher...

    Write a program that implements an elementary bit stream cipher. An elementary level bit stream cipher is an encryption algorithm that encrypts 1 byte of plain text at a time. This one uses a given 4-bit bit pattern as the key. The size of the encrypted message that we want to be able to send has a maximum length of 200 characters. You must: 1. prompt the user to input the clear text to be encrypted. You must use printf()...

  • C++ program to convert between decimal, hexadecimal, and octal. Please Help!!

    Hi, I need help writing a program that reads in data (hex, octal or decimal values) from an input file and outputs the values in to another base form (hex, octal,decimal) one line at a time depending on the formatting characters provided by the input file. I am posting the code requirements below and an example of what theinput file will look like and what should be output by the program. I only need the one .cpp program file. Thanks...

  • Write a C++ program that will read in image data from the file "image.txt" and illustrate...

    Write a C++ program that will read in image data from the file "image.txt" and illustrate that image as ASCII art. The Image file will contain several lines, representing horizontal rows in the image, and each line will have several integers in the range 0-255. representing povels, separated by spaces. You should read this image data into a vector vector in Once you've read in the phel data, go through the image and print out each piel in the image...

  • Lab #10 C++ Write a C++ program that reads text from a file and encrypts the...

    Lab #10 C++ Write a C++ program that reads text from a file and encrypts the file by adding an encryption factor (EF) to the ASCII value of each character. The encryption factor is 1 for the first line and increases by 1 for each line up to 4 and then starts over at 1. So, for the 4 th line the EF is 4, for the 5th line it is 1, for the 10th line it is 2. In...

  • Writing Unix Utilities in C (not C++ or C#) my-cat The program my-cat is a simple...

    Writing Unix Utilities in C (not C++ or C#) my-cat The program my-cat is a simple program. Generally, it reads a file as specified by the user and prints its contents. A typical usage is as follows, in which the user wants to see the contents of my-cat.c, and thus types: prompt> ./my-cat my-cat.c #include <stdio.h> ... As shown, my-cat reads the file my-cat.c and prints out its contents. The "./" before the my-cat above is a UNIX thing; it...

  • Summary: Write a C program that prompts the user to enter 2 positive integer numbers, display...

    Summary: Write a C program that prompts the user to enter 2 positive integer numbers, display the numbers in 3 formats. Then check, whether the larger of the 2 is evenly divisible by the smaller. Detail: Write a complete C program, more complex than the typical "hello world" program. Prompt the user to enter 2 integer numbers that are not negative. After either entry, display that number again. Print the smaller in hexadecimal, in decimal, and in octal format. Include...

  • Write a C program to run on ocelot to read a text file and print it...

    Write a C program to run on ocelot to read a text file and print it to the display. It should optionally find the count of the number of words in the file, and/or find the number of occurrences of a substring, and/or take all the words in the string and sort them lexicographically (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring]...

  • Write a C++ program that reads text from a file and encrypts the file by adding...

    Write a C++ program that reads text from a file and encrypts the file by adding 6 to the ASCII value of each character. See section 5.11 in Starting out with C++ for information on reading and writing to text files. Your program should: 1. Read the provided plain.txt file one line at a time. Because this file has spaces, use getline (see section 3.8). 2. Change each character of the string by adding 6 to it. 3. Write the...

  • How would I change the following C code to implement the following functions: CODE: #include <stdio.h>...

    How would I change the following C code to implement the following functions: CODE: #include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { if (argc != 2) printf("Invalid input!\n"); else { FILE * f = fopen (argv[1], "r"); if (f != NULL) { printf("File opened successfully.\n"); char line[256]; while (fgets(line, sizeof(line), f)) { printf("%s", line); } fclose(f); } else { printf("File cannot be opened!"); } } return 0; } QUESTION: Add a function that uses fscanf like this:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT