Question

Write a Hex Viewer method with the following header: private static void viewHex(String filename) The method...

Write a Hex Viewer method with the following header:

private static void viewHex(String filename)

The method reads bytes from filename and displays them in hex representation. The output should be formatted as in the example below, i.e., each line consists of 8 pairs of hex numbers, then ‘|’, then another 8 pairs. Use Integer.toHexString() to convert a byte into a string representing the equivalent hex. Use a try statement to handle IOException and display a simple error message if an I/O exception occurs.

Homework Answers

Answer #1
/**
 * @fileName HexViewer.java
 * @author
 * @since 28/2/17
 */


package hexviewer;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class HexViewer {

    /**
     * This method reads file as byte  and print as a hex String
     * @param fileName
     */
    public static void viewHex(String fileName) {
        Path path = Paths.get(fileName);
        byte[] data = new byte[0];
        try {
            data = Files.readAllBytes(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        int count = 1;
        for (byte b : data) {
            String hexString = Integer.toHexString(b);
//            System.out.println(String.format("%02X ", b));
            if (count == 8) {
                System.out.print(hexString + "| ");
                count = 0;
            } else {
                System.out.print(hexString + " ");
            }
            count++;
        }
         System.out.println();
    }

    public static void main(String[] args) {
        viewHex("input.txt");
    }
}

input.txt

output:

31 38 20 31 38 a 54 20| 50 20 49 20 52 20 43 20| 53 20 41 20 56 20 41 20| 4a 20 4c 20 45 20 58 20| 49 20 50 20 49 20 47 20| 45 a 4c 20 49 20 41 20| 4d 20 45 20 4d 20 4f 20| 52 20 59 20 4d 20 4d 20| 4f 20 55 20 53 20 45 20| 4e 20 49 20 4c a 43 20| 52 20 41 20 42 20 4b 20| 53 20 41 20 54 20 58 20| 49 20 4e 20 55 20 59 20| 48 20 53 20 54 20 46 20| 47 a 44 20 4e 20 44 20| 49 20 52 20 45 20 43 20| 54 20 4f 20 52 20 59 20| 45 20 54 20 41 20 4f 20| 45 20 4f 20 4f a 50 20| 4f 20 57 20 45 20 52 20| 53 20 55 20 50 20 50 20| 4c 20 59 20 4e 20 49 20| 52 20 46 20 52 20 4c 20| 4f a 55 20 43 20 4f 20| 41 20 53 20 41 20 45 20| 56 20 41 20 53 20 53 20| 43 20 52 20 45 20 54 20| 4e 20 44 20 47 a 4b 20| 49 20 52 20 4f 20 50 20| 4b 20 54 20 59 20 50 20| 53 20 48 20 52 20 55 20| 57 20 57 20 45 20 45 20| 4c a 43 20 44 20 44 20| 45 20 43 20 50 20 52 20| 45 20 45 20 41 20 48 20| 59 20 43 20 41 20 41 20| 54 20 52 20 4d a 41 20| 4e 20 52 20 49 20 4d 20| 41 20 4c 20 4c 20 54 20| 44 20 52 20 50 20 45 20| 52 20 52 20 45 20 41 20| 54 a 42 20 4f 20 4c 20| 45 20 4e 20 4d 20 45 20| 49 20 45 20 4b 20 45 20| 54 20 53 20 45 20 45 20| 50 20 48 20 48 a 52 20| 43 20 4b 20 49 20 50 20| 52 20 41 20 46 20 43 20| 56 20 52 20 49 20 49 20| 52 20 53 20 55 20 4c 20| 4d a 45 20 45 20 42 20| 45 20 49 20 41 20 52 20| 52 20 49 20 41 20 42 20| 4f 20 4f 20 54 20 4d 20| 42 20 4f 20 52 a 4e 20| 53 20 54 20 57 20 52 20| 41 20 50 20 52 20 47 20| 52 20 54 20 4e 20 57 20| 42 20 49 20 4e 20 47 20| 4f a 4e 20 4f 20 4f 20| 53 20 47 20 4e 20 44 20| 4c 20 4f 20 4f 20 44 20| 49 20 4e 20 54 20 49 20| 4f 20 49 20 53 a 41 20| 4e 20 47 20 4d 20 41 20| 4b 20 41 20 55 20 4c 20| 41 20 52 20 41 20 4f 20| 54 20 45 20 41 20 4e 20| 52 a 43 20 41 20 45 20| 41 20 53 20 50 20 54 20| 4c 20 54 20 41 20 49 20| 50 20 4f 20 4e 20 52 20| 4e 20 44 20 55 a 53 20| 4e 20 46 20 49 20 52 20| 45 20 57 20 41 20 4c 20| 4c 20 57 20 52 20 45 20| 49 20 4b 20 4f 20 4f 20| 43 a 54 20 46 20 44 20| 50 20 52 20 44 20 48 20| 54 20 4f 20 4f 20 54 20| 45 20 55 20 4c 20 42 20| 59 20 54 20 45 a 4a 41| 56 41 53 43 52 49 50 54| a 50 49 58 45 4c a 49| 4e 54 45 52 4e 45 54 a| 47 49 46 a 47 4f 4f 47| 4c 45 a 4c 43 44 a 45| 4d 41 49 4c a 4d 45 4d| 4f 52 59 a 4d 4f 55 53| 45 a 53 48 41 52 45 57| 41 52 45 a 54 41 53 4b| 42 41 52 a 55 4e 49 58| a 53 45 43 55 52 49 54| 59 a 53 4f 46 54 57 41| 52 45 a 46 4f 4c 44 45| 52 a 49 43 4f 4e a 44| 49 52 45 43 54 4f 52 59|

Know the answer?
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
Similar Questions
write a method with the following signature that reads the file with specified filename and prints...
write a method with the following signature that reads the file with specified filename and prints out the number of words and the number of lines in the file static void countWordsAndLines (string filename)
JAVA: Write a method with the following header to return a string format to represent the...
JAVA: Write a method with the following header to return a string format to represent the reverse order of the integer: public static String reverse(int number) For example, reverse(3456) returns 6543 and reverse(809340) returns 043908. Write a test program that prompts the user to enter an integer then displays its reversal. Convert integer to string and print reverse of integer as string. Do not use built-in toString. Use loop.
1. you will just write your name in bytes - once in UTF8 bytes, once in...
1. you will just write your name in bytes - once in UTF8 bytes, once in UTF16 bytes. Name :Andrew Submit: 1. Your .java file 2. A screenshot showing your code ran and gave the right output. ---- /** * In this program you will write your first name in bytes in 2 different encodings. * Then convert the byte array to a String and print it out. * * TODO in lecture: show students how I google to find...
using System; public static class Lab5 { public static void Main() { // declare variables int...
using System; public static class Lab5 { public static void Main() { // declare variables int inpMark; string lastName; char grade = ' '; // enter the student's last name Console.Write("Enter the last name of the student => "); lastName = Console.ReadLine(); // enter (and validate) the mark do { Console.Write("Enter a mark between 0 and 100 => "); inpMark = Convert.ToInt32(Console.ReadLine()); } while (inpMark < 0 || inpMark > 100); // Use the method to convert the mark into...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import java.io.*; //This imports input and output (io) classes that we use 3 //to read and write to files. The * is the wildcard that will 4 //make all of the io classes available if I need them 5 //It saves me from having to import each io class separately. 6 /** 7 This program reads numbers from a file, calculates the 8 mean (average)...
please can you make it simple. For example using scanner or hard coding when it is...
please can you make it simple. For example using scanner or hard coding when it is a good idea instead of arrays and that stuff.Please just make one program (or class) and explain step by step. Also it was given to me a txt.htm 1.- Write a client program and a server program to implement the following simplified HTTP protocol based on TCP service. Please make sure your program supports multiple clients. The webpage file CS3700.htm is provided. You may...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as possible: What is a checked exception, and what is an unchecked exception? What is NullPointerException? Which of the following statements (if any) will throw an exception? If no exception is thrown, what is the output? 1: System.out.println( 1 / 0 ); 2: System.out.println( 1.0 / 0 ); Point out the problem in the following code. Does the code throw any exceptions? 1: long value...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT