2

My output is reflecting the file that I am needing to process into hex values but my hex values are not being reflected in the output. Why isn't my file being converted into hex values?

public class HexUtilityDump {

public static void main(String[] args) {
    FileReader myFileReader = null;
    try {
        myFileReader = new FileReader("src/hexUtility/test.txt");
    } catch(Exception ex) {
        System.out.println("Error opening file: " + ex.getLocalizedMessage());
    }
    BufferedReader b = null; 
    b = new BufferedReader(myFileReader);

    //Loop through all the records in the file and print them on the console
    while (true){ 
        String myLine; 
        try {
            myLine = b.readLine();
            //check for null returned from readLine() and exit loop if so. 
            if (myLine ==null){break;} 
            System.out.println(myLine); 
        } catch (IOException e) {
            e.printStackTrace();
            //it is time to exit the while loop
            break; 
        }
    }
} 

Here is the code to pull the file through the conversion

public static void convertToHex(PrintStream out, File myFileReader) throws IOException {
    InputStream is = new FileInputStream(myFileReader);

    int bytesCounter =0;        
    int value = 0;
    StringBuilder sbHex = new StringBuilder();
    StringBuilder sbText = new StringBuilder();
    StringBuilder sbResult = new StringBuilder();

    while ((value = is.read()) != -1) {    

        //convert to hex value with "X" formatter
        sbHex.append(String.format("%02X ", value));

        //If the character is not convertible, just print a dot symbol "." 
        if (!Character.isISOControl(value)) {
            sbText.append((char)value);
        } else {
            sbText.append(".");
        }

        //if 16 bytes are read, reset the counter, 
        //clear the StringBuilder for formatting purpose only.
        if(bytesCounter==15) {
            sbResult.append(sbHex).append("      ").append(sbText).append("\n");
            sbHex.setLength(0);
            sbText.setLength(0);
            bytesCounter=0;
        }else{
            bytesCounter++;
        }
    }

    //if still got content
    if(bytesCounter!=0){            
        //add spaces more formatting purpose only
        for(; bytesCounter<16; bytesCounter++){
            //1 character 3 spaces
            sbHex.append("   ");
        }
        sbResult.append(sbHex).append("      ").append(sbText).append("\n");
    }

    out.print(sbResult);
    is.close();
}
0

1 Answer 1

1

You never call convertToHex, remove the file reading from your main() method. It appears you wanted to do something like,

File f = new File("src/hexUtility/test.txt");
convertToHex(System.out, f);
1
  • Thank you! I eliminated the initial file reading and it was smooth sailing! Sometimes an extra set of eyes is all you need to find the problem :)
    – mrsw
    Sep 17, 2014 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.