Write C Program Display Contents File Base 16 Hexadecimal Ascii Complete Following Tasks O Q43875081
Write a C program which will display the contents of a file inbase-16 (hexadecimal) and in ASCII.
Complete the following tasks:
- Obtain the name of the input file from the command line. If thecommand-line is “./hexdump xxx.bin” then argv[1] will contain“xxx.bin”.
- Open the file for binary input
- Print the entire file, 16-bytes per line. Each line shouldbegin with an 8-digit hexadecimal offset into the file. This is thecount of the bytes that you have already printed, and thereforebegins with 00000000. Each of the 16 bytes should be printed as a2-digit hexadecimal value separated by spaces. Print leading zerosif necessary to ensure 2 digits. After all 16 bytes are printed,display any values which are printable as ASCII characters, anddisplay any non-printable values as ‘.’. Therefore there will be 16symbols at the end of the line.
Some useful hints:
- An integer can be printed in hexadecimal by using the formatstring “%x”. If you want to fix the width of the printed value, putthe width before the x “%8x”. If you want leading zeros to ensureall values have the same width, put a 0 before this: “%08x”. Thisworks for any number of digits.
- To determine if a character is printable, include the header<ctype.h> and use the isprint(c) macro. This checks if thecharacter in the variable ‘c’ is printable, and returns true orfalse.
- To print a value as a character, use the “%c” formatstring.
- To print a linefeed, add “n” to your format string. If yourprintf does not have this linefeed, then the next printf willappend to the current line. This is how you can print severalvalues on a line in a loop.
- File I/O can be done using fopen and fread. Look at the manpages for these, “man 3 fopen”, “man 3 fread”
- You can check for end-of-file with feof().
Requirements:
- Your program should be named “hexdump.c” without thequotes.
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
Expert Answer
Answer to Write a C program which will display the contents of a file in base-16 (hexadecimal) and in ASCII. Complete the followin…
OR