开发者

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 */
            resul开发者_开发问答t = (byte)((result << 1) | (image[offset] & 1));
            }
        }
    return result;
}

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


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


You probably want:

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


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


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;


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;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜