Android的字节数组到字符串字节数组 [英] Android byte array to string to byte array

查看:166
本文介绍了Android的字节数组到字符串字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只需要转换的byte []为String。然后做一些与串并转换回byte []数组。但在这个测试我只是转换的byte []串并转换回的byte [],其结果是不同的。

All I need is convert byte[] to String. Then do something with that string and convert back to byte[] array. But in this testing I'm just convert byte[] to string and convert back to byte[] and the result is different.

要使用此字节[]转换为字符串:

to convert byte[] to string by using this:

byte[] byteEntity = EntityUtils.toByteArray(entity);
String s = new String(byteEntity,"UTF-8");

然后我尝试:

Then i tried:

byte[] byteTest = s.getBytes("UTF-8");

然后我complared它:

Then i complared it:

if (byteEntity.equals(byteTest) Log.i("test","equal");
else Log.i("test","diff");

所以,结果是不同的。

So the result is different.

我在搜索计算器关于这一点,但它不符合我的情况。问题的关键是我的数据是png格式图片,以便转换成字符串不可读。先谢谢了。

I searched in stackoverflow about this but it doesn't match my case. The point is my data is .png picture so the string converted is unreadable. Thanks in advance.

推荐答案

解决

使用这样的事情。

byte[] mByteEntity = EntityUtils.toByteArray(entity);
byte[] mByteDecrypted = clip_xor(mByteEntity,"your_key".getBytes());
baos.write(mByteDecrypted);
InputStream in = new ByteArrayInputStream(baos.toByteArray());

这是功能clip_xor

and this is function clip_xor

protected byte[] clip_xor(byte[] data, byte[] key) {
    int num_key = key.length;
    int num_data = data.length;

    try {
        if (num_key > 0) {
            for (int i = 0, j = 0; i < num_data; i++, j = (j + 1)
                    % num_key) {
                data[i] ^= key[j];
            }
        }
    } catch (Exception ex) {
        Log.i("error", ex.toString());
    }
    return data;
}

希望这将是有用的人面临着同样的问题。感谢您的所有帮助我解决这个问题。

Hope this will useful for someone face same problem. Thanks you your all for helping me solve this.

特别感谢P'krit_s

Special thanks for P'krit_s

这篇关于Android的字节数组到字符串字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆