java 在android中将字节数组转换为Base64字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4320269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
convert byte array to Base64 String in android
提问by Aakash
So I have the user enter password to signup from the android app.
所以我让用户输入密码从 android 应用程序注册。
Before I save the password to the database on the server I want to convert it to a MD5 one way hash and save it to the database.
在将密码保存到服务器上的数据库之前,我想将其转换为 MD5 单向哈希并将其保存到数据库中。
MessageDigest md = null;
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
try {
md.update(password.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
byte raw[] = md.digest();
How do I convert this byte array to a Base64 string. I saw in some forum that android util package left out the Base64 encoding and decoding on the other hand I see the encodetoString function in the android developers site.
如何将此字节数组转换为 Base64 字符串。我在某个论坛上看到 android util 包遗漏了 Base64 编码和解码,另一方面我在 android 开发人员站点中看到了 encodetoString 函数。
Any help is appreciated.
任何帮助表示赞赏。
采纳答案by Eric Levine
If you want your application to be compatible below API level 8, then you can pull in a 3rd part library to handle the Base64 encoding.
如果您希望您的应用程序在 API 级别 8 以下兼容,那么您可以引入第三部分库来处理 Base64 编码。
Apache Commons Codecis one you can try. It includes a Base64 codec: http://commons.apache.org/codec/api-release/index.html.
Apache Commons Codec是您可以尝试的一种。它包括一个 Base64 编解码器:http: //commons.apache.org/codec/api-release/index.html。
回答by Kevin Gaudin
For such a basic utility class, as there is a late implementation of Base64 in API 8, I would copy the latest source from the AOSP git repoto my own application package. It compiles with API level 3.
对于这样一个基本的实用程序类,由于 API 8 中有 Base64 的后期实现,我会将最新的源代码从 AOSP git repo复制到我自己的应用程序包中。它使用 API 级别 3 进行编译。
This way, you can code in your application with the same API as the official android Base64 implementation and stay compatible with all android versions. The day API 8 becomes the oldest version of android on the market, you just have to delete your version of the class in your package and update the imports to the official android.util.Base64.
这样,您可以使用与官方 android Base64 实现相同的 API 在您的应用程序中进行编码,并与所有 android 版本保持兼容。API 8 成为市场上最古老的 android 版本的那一天,您只需删除包中的类版本并将导入更新为官方 android.util.Base64。
Eclipse hint: just follow the link I provided to the source in your browser, Ctrl-A to select all the code, Ctrl-C to copy it, open Eclipse, click on the package where you want to create the class, hit Ctrl-V. The .java file is created automatically with the source directly modified with the correct package declaration.
Eclipse 提示:只需按照我在浏览器中提供的源链接,Ctrl-A 选择所有代码,Ctrl-C 复制它,打开 Eclipse,单击要创建类的包,按 Ctrl-五、.java 文件是使用正确的包声明直接修改的源自动创建的。
回答by Aakash
Here is how I did it... Just this code after the code above in my question.
这是我如何做到的......在我的问题中上面的代码之后就是这个代码。
StringBuffer hexString = new StringBuffer();
for (int i=0; i<raw.length; i++)
hexString.append(Integer.toHexString(0xFF & raw[i]));
password = hexString.toString();
Thanks.
谢谢。