我正在使用Android javax API加密一个字符串,该字符串返回一个字节数组,我再次将其转换为字符串(目的是稍后写入文本文件)。
现在使用这个字符串,我将其转换为要解密的字节数组,这将返回另一个字节数组,我再次将该数组转换为字符串。
我不能让它工作。我将问题缩小到字符串到字节数组部分的转换。因为如果我使用加密的字节数组来解密,然后得到字符串,它就可以工作。
不知道问题出在哪里。我使用了以下内容进行转换:
String str;
Byte [] theByteArray = str.getBytes("UTF-8");
String val = new String (theByteArray , "UTF-8");
and
Byte [] theByteArray = str.getBytes();
String val = new String (theByteArray);在不丢失任何东西的情况下,从字节数组转换为字符串的最佳方法是什么,反之亦然?
发布于 2012-11-23 11:51:29
发布于 2012-11-23 12:07:43
如果您确实需要另一种方法将字节数组存储为字符串,反之亦然,最好的方法是使用Base64 encoding,这样您就不会丢失任何数据。这是您可以下载压缩包的link。解压zip并将类文件包含在您的项目中。然后在需要编码和解码的地方使用以下代码片段。
//对数据进行编码并转换成字符串
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitMap.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte [] ba = bao.toByteArray();
String ba1=Base64.encodeBytes(ba);//再次将数据解码为字节数组
try{
byte[] ba3 = Base64.decode(ba1);
}catch(Exception e)
{
}https://stackoverflow.com/questions/13522690
复制相似问题