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 );
	   	    			NextLine = "";

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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin MillerCTOCommented:
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.
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: '?')
Please attach picture.hex
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Your main error is to use a Writer. You need FileOutputStream. Writer is for characters

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Erar nitoxAuthor Commented:
@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);
                        val = 0;
        } catch (IOException e) {

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Images and Photos

From novice to tech pro — start learning today.