close
Android Base 64 encode and decode example code
First:
-
Choose an encoding (UTF-8 is generally a good choice)
Transmitting end:
-
Encode the string to bytes (e.g. text.getBytes(encodingName))
-
Encode the bytes to base64 using the Base64 class
-
Transmit the base64
Receiving end:
-
Receive the base64
-
Decode the base64 to bytes using the Base64 class
-
Decode the bytes to a string (e.g. new String(bytes, encodingName))
EDIT: So something like:
// Sending
sidebyte[] data = text.getBytes("UTF-8");
String base64 = Base64.encodeToString(data, Base64.DEFAULT);
// Receiving
sidebyte[] data = Base64.decode(base64, Base64.DEFAULT);
String text = new String(data, "UTF-8");
文章標籤
全站熱搜