【发布时间】:2021-03-19 10:53:41
【问题描述】:
如何使用 Kotlin 将 ByteArray 转换为 Int?
我在 Java 中使用的代码:
return ((buffer[offset++] & 0xff) << 24) |
((buffer[offset++] & 0xff) << 16) |
((buffer[offset++] & 0xff) << 8) |
(buffer[offset] & 0xff);
我在 Kotlin 中尝试过这段代码:
return (buffer[offset++] and 0xff shl 24) or
(buffer[offset++] and 0xff shl 16) or
(buffer[offset++] and 0xff shl 8) or
(buffer[offset] and 0xff)
但它返回以下关于“and”运算符的警告:
以下候选人均不适用,因为接收者 类型不匹配
【问题讨论】:
-
如果我将
buffer[offset++]替换为值1,我似乎没有收到任何警告或错误,可能是您的缓冲区(或缓冲区内的类型)有问题) 也许?
标签: android kotlin type-conversion