Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用Kotlin将ByteArray转换为Int?_Android_Kotlin_Type Conversion - Fatal编程技术网

Android 如何使用Kotlin将ByteArray转换为Int?

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”运算符

如何使用Kotlin将ByteArray转换为Int

我在Java中使用的代码:

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)