在Android中如何连接的base64 EN codeD字符串?
问题描述:
请帮我解决这个问题:
我有两个字符串电子邮件ID和密码,如
String name = "xyz@gmail.com";
String pass = "abc";
我的连接code这两个成Base64的字符串,如
String encoded_name = new String(Base64.encode(name.getBytes(), 0));
String encoded_pass = new String(Base64.encode(pass.getBytes(), 0));
我需要连接这两个连接codeD字符串中包含空格
String merge = encoded_name + " " + encoded_pass;
我在控制台中检查该字符串
System.out.print("Concatenate string= " + merge);
但在控制台中我得到的结果在两行像这样
but in console I am getting result in two lines like this
11-18 00:25:29.898: INFO/System.out(1244): Merge= eHl6QGdtYWlsLmNvbQ==
11-18 00:25:29.908: INFO/System.out(1244): YWJj
这是为什么happing结果是意想不到的,我为什么不打印在一行。请帮我解决这个问题。
Why is this happing the result is unexpected for me why it is not printing in a single line. please help me to solve this.
感谢
答
您应该使用NO_WRAP标志作为的文档,Base64编码类将不会增加额外的新行。
You should use the NO_WRAP flag as described in the Docs, the Base64 class will not add additional newlines.
NO_WRAP:恩codeR标志位来省略所有行结束符(即输出将在一个长行)
NO_WRAP: Encoder flag bit to omit all line terminators (i.e., the output will be on one long line).
所以,改变你的行为以下内容:
So change your lines to the following:
String encoded_name = new String(Base64.encode(name.getBytes(), Base64.NO_WRAP));
String encoded_pass = new String(Base64.encode(pass.getBytes(), Base64.NO_WRAP));
这将输出以下内容:
11-17 19:16:51.283: INFO/System.out(354): Concatenate string= eHl6QGdtYWlsLmNvbQ== YWJj