Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
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? – David O'Meara May 8 '12 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). – Josephus Villarey May 8 '12 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. – majorl3oat May 9 '12 at 1:03
add comment (requires an account with 50 reputation)

3 Answers

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.

share|improve this answer
Thank you for your answer. but I tried as you suggested. Arrays.equals(byteEntity, byteTest) make same answer as it's not equal. – majorl3oat May 9 '12 at 1:13
add comment (requires an account with 50 reputation)

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.

share|improve this answer
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? – majorl3oat May 9 '12 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. – rosco May 9 '12 at 6:33
Oh, I see. Thanks you so much. I can solved it with xor algorithm. – majorl3oat May 9 '12 at 8:23
add comment (requires an account with 50 reputation)
up vote 0 down vote accepted

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

share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.