Converting byte array to base64 string java
Solution 1
have you tried with the encodeBase64String
method instead of using encodeBase64URLSafeString
?
encodeBase64URLSafeString:
Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output. The url-safe variation emits - and _ instead of + and / characters.
encodeBase64String:
Encodes binary data using the base64 algorithm but does not chunk the output. NOTE: We changed the behaviour of this method from multi-line chunking (commons-codec-1.4) to single-line non-chunking (commons-codec-1.5).
source : org.apache.commons.codec.binary.Base64 javadoc
Solution 2
Use
String sCert = javax.xml.bind.DatatypeConverter.printBase64Binary(pk);
Solution 3
This may be helpful
sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
encoder.encode(byteArray);
Comments
-
Suave Nti over 3 years
Trying to convert a
byte[]
to base64 string usingorg.apache.commons.codec.binary.Base64
..For this my java code looks like:base64String = Base64.encodeBase64URLSafeString(myByteArray);
But what i see is some invalid characters in the generated
base64
string..Why do I see these
____
lines in my generatedbase64
String? Is it a valid string? Note the length of the generated string is dividable by four.