Hex Dump back to .jpg (Java)

I had this question after viewing Conveting image in hex format to a jpeg file.
i tried to solve the problem listed above using java:
                       int content;
	   	       String NextLine = ""; 
	   	       Charset ansi = Charset.forName("Cp1252");
	   	       byte[] nextChar = new byte[1];

                        FileInputStream hexReader = new FileInputStream("./HEX/picture.hex");
	   	    	BufferedWriter pictureWriter = new BufferedWriter(new FileWriter("./Pictures/picture.jpg"));
	   	    	
	   	    	while((content = hexReader.read()) != -1){
	   	    		    NextLine += (char)content;
	   	    		    content = hexReader.read();
	   	    			NextLine += (char)content;
	   	    			
	   	    			nextChar[0] = (byte)((Character.digit(NextLine.charAt(0), 16) << 4) + Character.digit(NextLine.charAt(1), 16));
	   	    			NextLine = new String(nextChar, ansi );
					
	   	    			pictureWriter.append(NextLine);
	   	    			NextLine = "";
	   	    		
	   	    	}
	   	    	hexReader.close();
	   	    	pictureWriter.close();

Open in new window

I get a picture file using this method but the picture is currupted and glichy.
I suspect: The problem is that the characters 0x81, 0x8D, 0x8F, 0x90 and 0x9D are not supported by the Cp1252 charset

here are the files:
Original picture original .jpg file
original_picture.hex.txt original hex dump
result of the java code result (corrupted .jpg file)


Thanks for your help :)
Erar nitoxAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
Your main error is to use a Writer. You need FileOutputStream. Writer is for characters
1
 
Martin MillerConnect With a Mentor CTOCommented:
if you have access to Linux or MacOS

You can do an OD command, e.g.

/od -hex writer-clipart-k7292496.jpg

you can see the hex output... maybe this can help you debug the challenge.
0
 
Erar nitoxAuthor Commented:
@Martin Miller i did compare the hex dump of the original file with the dump of the resulting/corrupted file.
They differ whenever 81, 8D, 8F, 90 or 9D occure in the original dump. In the currupted file these values are just filled with 3F (char: '?')
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
CEHJConnect With a Mentor Commented:
Please attach picture.hex
0
 
Erar nitoxAuthor Commented:
@CEHJ thanks, i will try that later :) Thank you very much for the fast response.
0
 
CEHJCommented:
:)
Probably better to keep String out of it

    public static void makePic(String hexFile, String jpgFile) {
        final char ZERO = '0';
        try {
            try (Reader in = new FileReader(hexFile)) {
                try (OutputStream out = new FileOutputStream(jpgFile)) {
                    int buf = -1;
                    int numRead = -1;
                    int val = 0;
                    while ((buf = in.read()) > -1) {
                        if (buf != ZERO) {
                            val = Character.digit(buf, 16) << 4;
                        }

                        val |= Character.digit(in.read(), 16);
                        out.write(val);
                        val = 0;
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Open in new window

1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.