无法从byte []转换为byte

时间:2015-11-02 15:41:59

标签: java

此代码似乎在编译之前编译:

private static int addTagLengthValue(int i, int length, byte[] temp, byte[] buffer, int index) {
    buffer[index++] = i;
    buffer[index++] = (byte) length;
    if (length > 0)
        buffer[index++] = temp;
    return (index);
}

但现在我收到了这些错误:

类型不匹配:无法从byte []转换为byte 类型不匹配:无法从int转换为字节

我想我可以将i转换为byte但是如何处理temp参数?我只是维护代码并且出现了这些错误。是否应该使用不同版本的Java?

1 个答案:

答案 0 :(得分:1)

您遇到类型错误。变量temp应为byte,而不是byte[]。 是的,您可以将int转换为byte,就像那样

int i = 10;

byte b = (byte) i;

但是你应该确定你没有失去重要的一点。