Is there a base-64 decoder and encoder for a String in Android?
What Does Base64 Mean? Base64 is an encoding and decoding technique used to convert binary data to an American Standard for Information Interchange (ASCII) text format, and vice versa.
The base64 is a binary to a text encoding scheme that represents binary data in an ASCII string format. base64 is designed to carry data stored in binary format across the channels. It takes any form of data and transforms it into a long string of plain text.
Base64 encoding schemes are commonly used when there is a need to encode binary data that needs to be stored and transferred over media that are designed to deal with ASCII. This is to ensure that the data remain intact without modification during transport.
Basic Encoding and DecodingIt uses the Base64 alphabet specified by Java in RFC 4648 and RFC 2045 for encoding and decoding operations. The encoder does not add any line separator character. The decoder rejects data that contains characters outside the base64 alphabet.
This is an example of how to use the Base64
class to encode and decode a simple String value.
// String to be encoded with Base64 String text = "Test"; // Sending side byte[] data = null; try { data = text.getBytes("UTF-8"); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } String base64 = Base64.encodeToString(data, Base64.DEFAULT); // Receiving side byte[] data1 = Base64.decode(base64, Base64.DEFAULT); String text1 = null; try { text1 = new String(data1, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); }
This excerpt can be included in an Android activity.
See android.util.Base64
It seems that this was added in API version 8 or android 2.2 so it will not be available on the older platforms.
But the source of it is at android/util/Base64.java
so if needed one could just copy it unchanged for older versions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With