Android

How to encode / decode data in Android

Alex Zhukovich 1 min read
How to encode / decode data in Android
Table of Contents

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.

import android.util.Base64;

...

String testValue = "Hello, world!";

byte[] encodeValue = Base64.encode(testValue.getBytes(), Base64.DEFAULT);
byte[] decodeValue = Base64.decode(encodeValue, Base64.DEFAULT);

Log.d("ENCODE_DECODE", "defaultValue = " + testValue);
Log.d("ENCODE_DECODE", "encodeValue = " + new String(encodeValue));
Log.d("ENCODE_DECODE", "decodeValue = " + new String(decodeValue));


Output:

defaultValue = Hello, world!
encodeValue = SGVsbG8sIHdvcmxkIQ==
decodeValue = Hello, world!


References:


Mobile dev & testing with Alex Zhukovich

A blog about Android development & testing, Best Practices, Tips and Tricks

Share
Comments
More from Mobile dev & testing with Alex Zhukovich
Jetpack Compose: RadioButton
Jetpack Compose

Jetpack Compose: RadioButton

Many mobile apps ask a user to provide data, and RadioButton component can simplify the onboarding process and how users can fill the information. I'll show you the possibilities of "RadioButton", how to customize and test it in JetpackCompose.
Alex Zhukovich 9 min read

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Mobile dev & testing with Alex Zhukovich.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.