We help IT Professionals succeed at work.

Hex String to Image file

bigboy1122
bigboy1122 asked
on
Medium Priority
588 Views
Last Modified: 2011-10-03
Right now I use Oracles XDK to query the database and return an XML string.  The XML contains an element which is an image(comes from a Blob).  The data is in Hex format.  Is there a way to take a HEX string and convert it back to a JPEG?  I have tried converting the HEX to binary but its too big and will take too long.  Any help would be greatly appreciated.
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
byte[] imageBytes = new BigInteger(hexString).toByteArray();
// Create an image with the array
CERTIFIED EXPERT
Top Expert 2016

Commented:
Sorry

byte[] imageBytes = new BigInteger(hexString, 16).toByteArray();
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
// you can load it as an image using:

Image image = ImageIO.read(new ByteArrayInputStream(new BigInteger(hexString, 16).toByteArray()));

// or save it directly to a file using:

OutputStream out = new BufferedOutputStream(new FileOutputStream("image.jpg"));
out.write(new BigInteger(hexString, 16).toByteArray());
out.close();

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thanks guys. Just what I was looking for.
CERTIFIED EXPERT
Top Expert 2016

Commented:
Not sure why i received no credit for the answer accepted, since i provided most of it ...
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.