Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Base64 encoder and decoder

Is there a base-64 decoder and encoder for a String in Android?

like image 788
xydev Avatar asked Dec 01 '10 07:12

xydev


People also ask

What is Base64 encoding and decoding?

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.

Is Base64 an encoder?

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.

What is Base64 encoding used for?

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.

What is Base64 encoding and decoding in Java?

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.


2 Answers

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.

like image 191
blackpanther Avatar answered Oct 09 '22 11:10

blackpanther


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.

like image 37
Dan D. Avatar answered Oct 09 '22 09:10

Dan D.