aspose file tools*
The moose likes Java in General and the fly likes Converting String to Byte[] array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic
Author

Converting String to Byte[] array

Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 742
Hello,



The above code is throwing error of:
Incompatible type
found: byte[]
required: java.lang.byte[]

Thanks in anticipation
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

You will need to create a Byte[] and set all the elements using a loop.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 742
Wouter Oet wrote:You will need to create a Byte[] and set all the elements using a loop.



Same incompatible error

thanks again for helping
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

getBytes returns an array of bytes (primitive). To convert it into a Byte[] (wrapper) you'll need to create a new Byte[] and set the values of that.
Like this:


Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 742
Wouter Oet wrote:getBytes returns an array of bytes (primitive). To convert it into a Byte[] (wrapper) you'll need to create a new Byte[] and set the values of that.
Like this:



Hello,

Thanks for your reply.


OUTPUT:
vpc_SecureHash=5DB0F8628C4801B24110FD52056WEJY5
Converted to=1f8a39f895956b6ebf5ae9268f27cd27
Converted to Original Hash=[B@136a1a1

Please check it again as to how can I convert back the hash (md5)

Best regards
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

On the whole it is very difficult to see what your code is trying to do so it is not obvious to me what you are asking.

Are you asking how to take a message digest and recover the message? If so you can't because a message digest is a one way algorithm. There are sites that say they can do this but all they do is maintain a large database of mappings from messages to digests. There are an infinite number of possible messages but only a finite number of digest values (2^16 for MD5) so for each digest value there are an infinite number of messages that can be used to create it.

Are you asking why
Converted to Original Hash=[B@136a1a1
is not what you expect? If so you should note that the toString() method of an array (your println() uses it behind the scenes) does not in any way reflect the content of that array. You will need to hex encode the content if you expect to see hex.

Please note - the line
does not preserve leading zeros so is a very bad way of converting a digest to a hex string. You would do better to use one of the open source libraries such as that from the Jakarta Codec project.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Farakh khan wrote:Please check it again as to how can I convert back the hash (md5)
You can't convert it back. It's a hash. That's basically one way encryption.
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 742
Wouter Oet wrote:
Farakh khan wrote:Please check it again as to how can I convert back the hash (md5)
You can't convert it back. It's a hash. That's basically one way encryption.


Thanks for replies

If this is one way encryption then what is mean by comparing has?

Best regards
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42929
    
  68
Let's continue the discussion in this duplicate thread, which is largely identical.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Converting String to Byte[] array