Link to home
Start Free TrialLog in
Avatar of Erar nitox
Erar nitox

asked on

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:
User generated image original .jpg file
original_picture.hex.txt original hex dump
User generated image result (corrupted .jpg file)


Thanks for your help :)
SOLUTION
Avatar of Martin Miller
Martin Miller
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Erar nitox
Erar nitox

ASKER

@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: '?')
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@CEHJ thanks, i will try that later :) Thank you very much for the fast response.
:)
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