Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Hex to jpeg conversion  RSS feed

 
Ziggy Kowalski
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm having problems converting my image back to jpeg format. The code i used to get from jpeg to hex is as follows:

where:
ba is a byte[] which contains the image in jpeg format
ret is a string buffer, which stores the image in hex format.

i can't work out how to convert back to jpeg format

thank you in advance
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you converting a byte[] into a StringBuffer/String? Seems odd to me. Anyway, one way to reverse a process is to reverse each step. For example, how can you convert "A0" back to a byte? Take a look at the methods of Integer. In the final step, to convert a byte[] to an Image, take a look at class javax.imageio.ImageIO, or see if whatever you used to convert an image in jpeg format to a byte[] can be reversed.
 
Ziggy Kowalski
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, i can't get past step 1 :/

I've looked at the Integer class but i still can't work out how to (for eg) convert "A0" to a byte.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at Integer.parseInt

Why are you converting an image to a String?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"zzdziarski zzdziarski,"

Welcome to JavaRanch!

Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names.

You can edit your name here.

Thank you for your prompt attention, and enjoy the ranch!

-Marc
 
Ziggy Kowalski
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Albertson:
Take a look at Integer.parseInt

Why are you converting an image to a String?


I'm converting an image to a string because i'm passing the image to javascript which in turn passes the image to a hidden field for php to parse and manipulate as it pleases. I can't use a simple POST method in the applet because the number of applets on the page is specified by the user dynamically, and there are many other fields which constitute the page's form.

And Integer.parseInt looks promising. thanks
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the standard approaches to encoding/decoding binary information as text is to use the "Base 64" codec. There are plenty of implementations of that around, for example: http://jakarta.apache.org/commons/codec/apidocs/org/apache/commons/codec/binary/Base64.html
 
Ziggy Kowalski
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:
"zzdziarski zzdziarski,"

Welcome to JavaRanch!

Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names.

You can edit your name here.

Thank you for your prompt attention, and enjoy the ranch!

-Marc



No problem, however, if i recall correctly, the only policy shown to me when signing up for this forum was "Be nice". There was nothing about the types of names that we could select
 
Ziggy Kowalski
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Albertson:
One of the standard approaches to encoding/decoding binary information as text is to use the "Base 64" codec. There are plenty of implementations of that around, for example: http://jakarta.apache.org/commons/codec/apidocs/org/apache/commons/codec/binary/Base64.html


I'm using the Base 64 implementation shown above but i've encountered something weird. I thought base64 encoding returns only digits whereas i'm getting all sorts of stuff. For example: /9j/4AAQSkZJRg (that's the beginning of an encoded jpeg image). The encoding and decoding works fine, but i'm asking because another part of the system is going to have to use the encoded data and decode it itself. I don't want any problems to pop-up when that happens else my necks on the chopping block

thank you
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's okay. Think about it: in hex (base 16) you need 16 digits: 0-9 plus A-F. In base 64 you need 64 digits: 0-9 plus A-Z plus a-z plus '+' and '/' (usually). You can read more about it on Wikipedia: http://en.wikipedia.org/wiki/Base64
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!