Android 如何使用Kotlin将ByteArray转换为Int?
如何使用Kotlin将ByteArray转换为Int 我在Java中使用的代码:Android 如何使用Kotlin将ByteArray转换为Int?,android,kotlin,type-conversion,Android,Kotlin,Type Conversion,如何使用Kotlin将ByteArray转换为Int 我在Java中使用的代码: return ((buffer[offset++] & 0xff) << 24) | ((buffer[offset++] & 0xff) << 16) | ((buffer[offset++] & 0xff) << 8) | (buffer[offset] & 0xff); 但它返回有关“and”运算符
return ((buffer[offset++] & 0xff) << 24) |
((buffer[offset++] & 0xff) << 16) |
((buffer[offset++] & 0xff) << 8) |
(buffer[offset] & 0xff);
但它返回有关“and”运算符的以下警告:
由于接收人的原因,以下候选人均不适用
类型不匹配
多亏了@a_local_nobody,我成功地解决了这个问题:
return (buffer[offset++].toInt() and 0xff shl 24) or
(buffer[offset++].toInt() and 0xff shl 16) or
(buffer[offset++].toInt() and 0xff shl 8) or
(buffer[offset].toInt() and 0xff)
我做了一些阅读,似乎像
和
,或
,和shl
这样的位操作在Kotlin中只定义为Int和Long
从:
如果你想使用它们,你必须转换它们如果我用
1
值替换buffer[offset++]
,我似乎没有收到任何警告或错误,可能是你的缓冲区(或缓冲区内的类型)有问题?啊,告诉我:)@a_local_没人你做了所有的工作:)
return (buffer[offset++].toInt() and 0xff shl 24) or
(buffer[offset++].toInt() and 0xff shl 16) or
(buffer[offset++].toInt() and 0xff shl 8) or
(buffer[offset].toInt() and 0xff)