How to encode / decode data in Android ?

 

How to encode / decode data in Android ?


How to encode / decode data in Android ?


Sometimes need to encode/decode some data. A simple way to do it is to use Base64 Encoder, which is available in Android SDK since API 8. This class contains methods for encoding and decoding the Base64 representation of binary data.

We can use the following code to encode/decode any String value.


Encoding

  1. Encode the string to bytes (e.g. text.getBytes(encodingName))
  2. Encode the bytes to base64 using the Base64 class
  3. Transmit the base64



Decoding

  1. Receive the base64
  2. Decode the base64 to bytes using the Base64 class
  3. Decode the bytes to a string (e.g. new String(bytes, encodingName))


Post a Comment

Post a Comment (0)

Previous Post Next Post