2

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");

Then i complared it:

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

So the result is different.

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.

3
  • you're using Array.equals which isn't doing what you think it's doing. Have you tried converting back to Strings and comparing those values? Commented May 8, 2012 at 7:02
  • Can you elaborate as to why you're doing this? To be specific, what are you doing with the unreadable string? Images are better off stored as base64-encoded strings, if that's what you're trying to do). Commented May 8, 2012 at 7:03
  • My point is I have image that encrypted on server and when i get this image by using response = httppost.execute() -> entity = response.getEntity -> byte[] byteEntity = EntityUtils.toByteArray(entity) then i have to convert this byte[] to string then xor with key for decryption (Actually i tried to make decryption that not convert to string, but unfortunately it's not work.) after that i have to convert it to byte[] again to make it as ByteArrayOutputStream if you have any idea about the better method, very appreciate. Commented May 9, 2012 at 1:03

3 Answers 3

1

Solved

Using something like this.

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

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.

Special thanks for P'krit_s

0

primitive arrays are actually Objects (that's why they have .equals method) but they do not implement the contract of equality (hashCode and equals) needed for comparison. You cannot also use == since according to docs, .getBytes will return a new instance byte[]. You should use Arrays.equals(byteEntity, byteTest) to test equality.

1
  • Thank you for your answer. but I tried as you suggested. Arrays.equals(byteEntity, byteTest) make same answer as it's not equal. Commented May 9, 2012 at 1:13
0

Have a look to the answer here.

In that case my target was transform a png image in a bytestream to display it in embedded browser (it was a particular case where browser did not show directly the png).

You may use the logic of that solution to convert png to byte and then to String.

Then reverse the order of operations to get back to the original file.

3
  • Thank you for your answer. but what is this line mean : char[] base64_1 = MyUtilClass.Base64.encode(byt_1); what is MyUtilClass and in my project Base64 not have method Base64.encode but have Base64.encodeBytes(byte[] source) and it return as string. Is this same function? Commented May 9, 2012 at 1:22
  • I think that Base64.encodeBytes(byte[] source) may have the same behaviour of Base64.encode. The example I provided you is in J2ME and my intent was to give you an algorithm. Commented May 9, 2012 at 6:33
  • Oh, I see. Thanks you so much. I can solved it with xor algorithm. Commented May 9, 2012 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.