## Homework 5 Hexadecimal Core Dump Utility

### Outcomes

After successfully completing this assignment, you will...
• know how to process binary files
• be able to read and process command-line arguments
• know how to use the C bitwise operators
• be familiar with the hexadecimal representation of numbers
• know how to use `make` to organize, compile, and run your programs

### Before Starting

I will not be covering the material on bitwise operators, hexadecimal numbers, or command-line arguments in class. One of the skills you need to develop in order to become successful programmer is to be able to understand and put into practice what you read in a programming manual or textbook. Here are the relevant sections for this assignment:
• Read Chapter 11 on Files.
• Read section 14.4 on Using Command-Line Arguments.
• Read section 10.9 on Bitwise Operators.
• Read sections C.1 and C.2 (Appendix C) on Hexadecimal Numbers.

## The Assignment

We talked about the differences between text files and binary files in class. One major difference is that unlike text files, binary files cannot be viewed with a text editor. However, sometimes you want to be able to view the information in a binary file. One way to do this is to run a hex dump utility on the file, which displays each byte in the file as a two-digit hexadecimal value (try running the Linux utility `xxd` on one of your recent a.out files to see an example of a hex dump utility's output). In this assgnment, you will write a simple version of a hex dump program.

Here are the specifications for the program:

• Name your main program hexdump.c
• Your program should be able to take zero or one command-line arguments. If the program is invoked with zero arguments, assume the name of the binary file being read will be supplied as redirected input, for example,
```  hexdump < binaryFileName
```
If the program is invoked with one argument, assume that argument is the name of the binary file:
```  hexdump binaryFileName
```
If the program is invoked with two or more arguments, print an error message and exit the program.

• The contents of the binary file should be displayed on the screen in the following format: each byte of the file should be displayed as a two-digit hexadecimal number, 16 numbers per line, each number separated by a space. The letters 'A' - 'F' in a hexadecimal number should be displayed in uppercase. Here's a sample output that meets these specifications:
```04 AD 67 41 99 95 5A 62 BC FF FF FF 00 00 00 00
00 00 91 61 A1 42 77 6B 00 00 1E 29 47 F1 00 D4
```
• If the number of bytes in the file is not evenly divisible by 16, then the last line of output should only contain the bytes within the file (all preceding lines of output will still contain 16 numbers). Here's a sample output that meets this specification:
```39 24 5A 6B 99 00 00 00 AD A3 32 27 2D 8E 00 12
6E FF FE 19
```
• You must write a function called `byteToHex` that satisfies the following prototype and pre- and post-conditions:
```void byteToHex (char ch, char *hexDigits);

PRE:  hexDigits is a string (character array) with at least 3 elements
POST: hexDigits[0] contains an ASCII character representing the high-order
4 bits of ch as a hexidecimal value.
hexDigits[1] contains an ASCII character representing the low-order
4 bits of ch as a hexidecimal value.
hexDigits[2] is the null character (string terminator)
```
For example, if `byteToHex` receives the character 'Z' as `ch`, then upon return from the function, the value in hexDigits[0] would be 53 (decimal) and the value in hexDigits[1] would be 65 (decimal), representing the characters in the string "5A". (Look at the ASCII chart in Appendix C. The character 'Z' has ASCII code 90 (decimal). 90 (decimal) is equivalent to 5A (hexidecimal).)

• The prototype and pre- and post-conditions for byteToHex should appear in a header file called byteToHex.h. The function definition for `byteToHex` should appear in a file named byteToHex.c

• You should provide a makefile containing appropriate documentation that builds your program.

• stdio.h

### Hints

Get `byteToHex` working first. The debugger can be very helpful as you develop this function that uses the C bitwise operators. Write a simple main function to test `byteToHex`. Once you're sure that `byteToHex` works, then develop the main function to open a binary file, and call `byteToHex` to process each byte of the file.

## Deliverables

Submit your file using the following turnin command:

/cs/bin/turnin submit cs2301 PROJECT5 hexdump.c byteToHex.h byteToHex.c makefile README

Programs submitted after 5pm on December 7 will be tagged as late, and will be subject to the late homework policy.