Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CIT239 Java Programming Lab 17a Hex Dump Due Date You must demonstrate the solut

ID: 3834485 • Letter: C

Question

CIT239 Java Programming Lab 17a Hex Dump Due Date You must demonstrate the solution for this lab exercise to the instructor by Sunday, April 23,2017, in order to receive credit for this work. Converting Binary Data to "Hexadecimal Text" In this lab exercise, you will write a text-based application which will input a binary file and output a plain text file displaying the hexadecimal values of each byte from the input file. Input File Format Binary (even if the actual input file is a "txt" format). output File Format Plain text. The input data is converted to an ASCII representation of the binary input. This should not be UTF-8 format. While a tool such as this is useful for viewing real binary files, it can also be interesting to view a TEXT file using a binary file viewer. For example, the alphabet.txt file displayed below, results in the alphabet DUMP .txt file alphabet. txt abcdef ghij klmnopgrst ABCDEFGHIJ KLMNOPORST UVWXYZ alphabet DUMP.txt 0000 (0000) 61 62 63 64 65 66 67 68 0008 (0008 69 6A 0D OA 6B 6C 6D 6E 0016 (0010 6F 70 71 72 73 74 0D 0A 0024 0018 75 76 77 78 79 7A OD OA z Vr An. 0032 (0020) 41 42 43 44 45 46 4 7 48 0040 (0028 49 4A 0D 0A 4B 4C 4 D 4E 0048 (0030 4F 50 51 52 53 54 0D 0A T Nr An 0056 (0038 55 56 57 58 59 5A 0D OA An Column Definitions of the output File The output file for this program is a plain text representation of the binary input file. Each line of the output text includes: The file offset, in decimal. (This is the 4-digit value at the leftmost end of the line The file offset in hexadecimal. (This is the 4-digit value within the parentheses.) The translation of 8 binary bytes to hexadecimal text. (Each binary byte is represented by two hex digits, followed by a space.) e The display of those same 8 bytes as "displayable text if possible (separated by three spaces. CIT239 SU Lab17a HexDump 20170409.docx. 4/8/2017 8:33 PM

Explanation / Answer

The hexadecimal digits are the ordinary, base-10 digits '0' through '9' plus the letters 'A' through 'F'. In the hexadecimal system, these digits represent the values 0 through 15, respectively. Write a function named hexValue that uses a switch statement to find the hexadecimal value of a given character. The character is a parameter to the function, and its hexadecimal value is the return value of the function. You should count lower case letters 'a' through 'f' as having the same value as the corresponding upper case letters. If the parameter is not one of the legal hexadecimal digits, return -1as the value of the function.

A hexadecimal integer is a sequence of hexadecimal digits, such as 34A7, ff8, 174204, or FADE. If str is a string containing a hexadecimal integer, then the corresponding base-10 integer can be computed as follows:

Of course, this is not valid if str contains any characters that are not hexadecimal digits. Write a program that reads a string from the user. If all the characters in the string are hexadecimal digits, print out the corresponding base-10 value. If not, print out an error message.

Discussion

The subroutine has a parameter of type char and a return value of type int. It's easy to write the switch statement, although it's tedious because of the number of cases. A little creative cut-and-paste can help. The switch statement has a default case that covers all the characters that are not hexadecimal digits. For such characters, a value of -1 is returned. The subroutine is shown in the program below, and I will not repeat it here. Note that in this switch statement, the cases are terminated by return statements, rather than break.

In the main program, I will use TextIO.getlnWord() to read the user's input, rather than TextIO.getln(). This has the advantage that it will return a non-empty string that is guaranteed not to contain any blanks. We still have the problem of checking whether the user's input contains only valid hexadecimal digits. One approach is to check all the characters first and use a boolean variable to record whether they are all valid. We can test whether an individual character, ch, is valid by checking whether hexValue(ch) is -1. Let hex be a String holding the user's input. Then we have:

This works, but we have to process the string twice. We can avoid this by checking the input at the same time that we do the conversion. If the input is illegal, I want to end the program. I use the fact that a return statement in the main() routine will end the program, since it returns control back to the system:

This is the code that is used in the main() routine of the program to do the conversion. Note that I declared dec to be of type long to allow bigger values than would fit in a variable of type int. The program still has a problem if the user enters too many characters. (It gets the wrong answer.)

It would probably be better to write a function to do the conversion of a string to base-10. It could return a -1 if the string is not a legal hexadecimal number. The main() routine could then call the function and check its return value to find out whether there was an error.

In this exercise, a special value, -1, is returned by a subroutine to indicate that its parameter value is not valid. An alternative approach would be to throw an exception if the parameter value is not legal. Instead of saying "return -1", the default case of the switch statement in the subroutine could read:

The main program would then do the conversion from string to hexadecimal in a try..catch statement. If an exception occurs, the catch part of the statement can print an error message:

I think that this approach is much neater!

The Solution