Reputation: 111
Is there any way to convert a Base64 String like you can in .NET using Java? How can I achieve this?
Here is the .NET example :
Convert.FromBase64String
Upvotes: 2
Views: 8067
Reputation: 601
I find it curious that every time someone asks about base 64 encoding/decoding in Java the only answers given are either use the Apache codecs library, use the old com.sun classes which shouldn't be used or assume it is in the application server such as the one provided by Tomcat.
The standard JDK has provided base 64 support since the introduction of Jaxb 1.0 in the javax.xml.bind.DatatypeConverter class.
So if you are using Java 6 or newer (when Jaxb1.0 was added to the JDK directly, was available as a ref prior to that) you don't need to use a 3rd party component for base 64 support, you can use what is built into the JDK.
Upvotes: 5
Reputation: 18064
byte[] decodedBytes = Base64.decode(base64Message);
String decodedString = new String(decodedBytes);
Upvotes: 1
Reputation: 8101
I think you want to decode a base64encoded String...you can use this..
BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes = decoder.decodeBuffer(yourString.getBytes());
Upvotes: 0