Bookmark Topic Watch Topic
  • New Topic

Converting String to Byte[] array

 
Farakh khan
Ranch Hand
Posts: 833
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hello,



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

Thanks in anticipation
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
You will need to create a Byte[] and set all the elements using a loop.
 
Farakh khan
Ranch Hand
Posts: 833
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 833
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 781
Java Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 833
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 1
  • Mark post as helpful
  • send pies
  • Report post to moderator
Let's continue the discussion in this duplicate thread, which is largely identical.
 
I agree. Here's the link: http://aspose.com/file-tools
    Bookmark Topic Watch Topic
  • New Topic