apoorva
apoorva

Reputation: 11

Java: Error "Incompatible datatypes"

private byte[] decode_text(byte[] image)
{
    int length = 0;
    int offset = 32;
    for(int i=0; i<32; ++i)
    {
        length = (length << 1) | (image[i] & 1);
    }
    byte[] result = new byte[length];
    for(int b=0; b<result.length; ++b )
    {
        for(int i=0; i<8; ++i, ++offset)
            {
            /* I'm getting error at the following line */
            result = (byte)((result << 1) | (image[offset] & 1));
            }
        }
    return result;
}

Error is incompatible datatypes...required byte[] and found byte..........

Upvotes: 1

Views: 639

Answers (5)

user85421
user85421

Reputation: 29680

You probably want to do something like

    byte[] result = new byte[length];
    for(int b=0; b<result.length; ++b )
    {
        byte value = 0;
        for(int i=0; i<8; ++i, ++offset)
        {
            /* I'm getting error at the following line */
            value = (byte) ((value << 1) | (image[offset] & 1));
        }
        result[b] = value;
    }

Upvotes: 0

Peter Perh&#225;č
Peter Perh&#225;č

Reputation: 20782

You are casting the result of all these operations

((result << 1) | (image[offset] & 1));

to (byte) and assigning it to byte[].

You could declare a new byte variable, do you manipulations on that variable and then do

result[i] = myNewByteVariable;

Upvotes: 0

stefita
stefita

Reputation: 1793

Also you cannot assign a single byte to a byte-Array.

Upvotes: 1

Mark Byers
Mark Byers

Reputation: 838226

You probably want:

result[b] = (byte)((result[b] << 1) | (image[offset] & 1));

Upvotes: 1

Jonas Elfstr&#246;m
Jonas Elfstr&#246;m

Reputation: 31428

You can't bit shift the result variable because it's a byte array.

Upvotes: 1

Related Questions