有谁知道如何使用Android上的Base64类脱code和EN codeA字符串中的Base64?请帮助一些例如code。
你能不能给我确切的code;这将是很大的帮助。我现在用的是下面的code,但它不工作。
字符串源=密码;
字节[]的字节数组= source.getBytes(UTF-16);
Base64的BS =新的Base64();
//bs.en$c$cBytes(byteArray);
的System.out.println(bs.en codeBytes(字节阵列));
//bs.de$c$c(bs.en$c$cBytes(byteArray));
的System.out.println(bs.de code(bs.en codeBytes(字节阵列)));
解决方案
第一:
选择一个编码(UTF-8通常是一个不错的选择)发送端:
恩code中的字符串字节(如 text.getBytes(encodingName)
)
恩code。使用的Base64
类的字节为base64
在传输中的Base64
接收端:
在收到的base64 使用的Base64
C类的base64德$ C $为字节
在德$ C C字节$为一个字符串(如新的字符串(字节,encodingName)
)
编辑:所以是这样的:
//发送方
byte []的数据= text.getBytes(UTF-8);
字符串的base64 = Base64.en codeToString(数据,Base64.DEFAULT);
//接收方
byte []的数据= Base64.de code(BASE64,Base64.DEFAULT);
字符串文本=新的String(数据,UTF-8);
Does anyone know how to decode and encode a string in Base64 using the Base64 class in Android? Please help with some example code.
Can you give me exact code; that would be great help. I am using the following code, but it's not working.
String source = "password";
byte[] byteArray = source.getBytes("UTF-16");
Base64 bs = new Base64();
//bs.encodeBytes(byteArray);
System.out.println( bs.encodeBytes(byteArray));
//bs.decode(bs.encodeBytes(byteArray));
System.out.println(bs.decode(bs.encodeBytes(byteArray)));
解决方案
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 theBase64
class
Decode the bytes to a string (e.g. new String(bytes, encodingName)
)
EDIT: So something like:
// Sending side
byte[] data = text.getBytes("UTF-8");
String base64 = Base64.encodeToString(data, Base64.DEFAULT);
// Receiving side
byte[] data = Base64.decode(base64, Base64.DEFAULT);
String text = new String(data, "UTF-8");