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:
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 .jpg file
original_picture.hex.txt original hex dump
result (corrupted .jpg file)
Thanks for your help :)
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();
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 .jpg file
original_picture.hex.txt original hex dump
result (corrupted .jpg file)
Thanks for your help :)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@CEHJ thanks, i will try that later :) Thank you very much for the fast response.
:)
Probably better to keep String out of it
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();
}
}
ASKER
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: '?')