Android Kotlin 字节移位 +=
Android Kotlin Byte Shifting +=
我正在尝试将 BLE 本机字节处理助手转换为 Kotlin。在此过程中,我注意到
JAVA
myByteArray[offset] += (byte)(exponent & 0xFF);
它按预期工作,但是当转换为 kotlin 时
KOTLIN
myByteArray[offset] += (exponent and 0x0F shl 4).toByte()
我收到错误消息,提示应为整数。
所以我假设
"+="
是我的问题,导致假定为整数。
所以我有两个问题。
1) += 究竟对字节做了什么。我明白
b += 1
相当于
b = (byte)(b + 1)
但是字节到底发生了什么。是否正在进行转换,或者是否正在转换为 int,将值相加然后返回到字节?
2) Kotlin 中的等价物是什么,为什么它在 Kotlin 中失败。我也试过
myByteArray[offset] = myByteArray[offset] + (exponent and 0x0F shl 4).toByte()
郑重声明,如果您好奇的话,这是将整数值转换为 32 位浮点数。不确定这是否有帮助。
如果您对此感兴趣,完整代码是:
mantissa = intToSignedBits(mantissa, 12)
exponent = intToSignedBits(exponent, 4)
myByteArray[offset++] = (mantissa and 0xFF).toByte()
myByteArray[offset] = (mantissa shr 8 and 0x0F).toByte()
myByteArray[offset] += (exponent and 0x0F shl 4).toByte() //hates this line
注意* 它说
this
因为我把它写成一个 ByteArray 扩展,所以把它当作 byteArray 本身。
它的 KOTLIN 版本(错误)
JAVA 版本(无错误):
我想要的不仅仅是一个答案,但我会接受的,哈哈。我想更多地了解这里发生的事情,以便将来我也可以自己解决这个问题。提前感谢任何花时间解释和帮助的人。
我们还在讨论这个问题 ;)。
private int unsignedByteToInt(byte b) {
return b & 0xFF;
}
为什么这在 Java 中有效,但在 Kotlin 中失败。
private fun unsignedByteToInt(b: Byte): Int {
return b and 0xFF
}
老实说,我厌倦了编写 Java 帮助程序 类 来处理字节,所以我想弄清楚 Kotlin 字节处理的习语。 "and" 似乎只有 Int 的重载,字节在 Kotlin 中不被视为 Int。
作为一个额外的问题,如果你能解释一下,我也将不胜感激。
我相信你应该使用 Byte#plus(Byte)。
所以在你的情况下:
myByteArray[offset] = myByteArray[offset].plus((exponent and 0x0F shl 4).toByte())
.toByte()
在这种情况下可能不是必需的,因为 plus()
基本上可以接受任何数字,但我无法对其进行测试。
您是否注意到分解语句有效?
val newBytes = (myByteArray[offset]+(exponent and 0x0F shl 4)).toByte()
myByteArray[offset] = newBytes
此行为的罪魁祸首是 plus
操作员签名。这些都是 plus
for Byte
:
的所有重载
/** Adds the other value to this value. */
public operator fun plus(other: Byte): Int
/** Adds the other value to this value. */
public operator fun plus(other: Short): Int
/** Adds the other value to this value. */
public operator fun plus(other: Int): Int
/** Adds the other value to this value. */
public operator fun plus(other: Long): Long
/** Adds the other value to this value. */
public operator fun plus(other: Float): Float
/** Adds the other value to this value. */
public operator fun plus(other: Double): Double
None 其中 return 一个 Byte
并且 plusAssign
没有重载,因此它们是隐式创建的。
首先它执行 plus(Byte)
returning Int
然后尝试赋值,但它需要一个 Byte
所以它会导致你的错误。
我正在尝试将 BLE 本机字节处理助手转换为 Kotlin。在此过程中,我注意到
JAVA
myByteArray[offset] += (byte)(exponent & 0xFF);
它按预期工作,但是当转换为 kotlin 时 KOTLIN
myByteArray[offset] += (exponent and 0x0F shl 4).toByte()
我收到错误消息,提示应为整数。 所以我假设
"+="
是我的问题,导致假定为整数。 所以我有两个问题。
1) += 究竟对字节做了什么。我明白
b += 1
相当于
b = (byte)(b + 1)
但是字节到底发生了什么。是否正在进行转换,或者是否正在转换为 int,将值相加然后返回到字节?
2) Kotlin 中的等价物是什么,为什么它在 Kotlin 中失败。我也试过
myByteArray[offset] = myByteArray[offset] + (exponent and 0x0F shl 4).toByte()
郑重声明,如果您好奇的话,这是将整数值转换为 32 位浮点数。不确定这是否有帮助。
如果您对此感兴趣,完整代码是:
mantissa = intToSignedBits(mantissa, 12)
exponent = intToSignedBits(exponent, 4)
myByteArray[offset++] = (mantissa and 0xFF).toByte()
myByteArray[offset] = (mantissa shr 8 and 0x0F).toByte()
myByteArray[offset] += (exponent and 0x0F shl 4).toByte() //hates this line
注意* 它说
this
因为我把它写成一个 ByteArray 扩展,所以把它当作 byteArray 本身。
它的 KOTLIN 版本(错误)
JAVA 版本(无错误):
我想要的不仅仅是一个答案,但我会接受的,哈哈。我想更多地了解这里发生的事情,以便将来我也可以自己解决这个问题。提前感谢任何花时间解释和帮助的人。
我们还在讨论这个问题 ;)。
private int unsignedByteToInt(byte b) {
return b & 0xFF;
}
为什么这在 Java 中有效,但在 Kotlin 中失败。
private fun unsignedByteToInt(b: Byte): Int {
return b and 0xFF
}
老实说,我厌倦了编写 Java 帮助程序 类 来处理字节,所以我想弄清楚 Kotlin 字节处理的习语。 "and" 似乎只有 Int 的重载,字节在 Kotlin 中不被视为 Int。
作为一个额外的问题,如果你能解释一下,我也将不胜感激。
我相信你应该使用 Byte#plus(Byte)。
所以在你的情况下:
myByteArray[offset] = myByteArray[offset].plus((exponent and 0x0F shl 4).toByte())
.toByte()
在这种情况下可能不是必需的,因为 plus()
基本上可以接受任何数字,但我无法对其进行测试。
您是否注意到分解语句有效?
val newBytes = (myByteArray[offset]+(exponent and 0x0F shl 4)).toByte()
myByteArray[offset] = newBytes
此行为的罪魁祸首是 plus
操作员签名。这些都是 plus
for Byte
:
/** Adds the other value to this value. */
public operator fun plus(other: Byte): Int
/** Adds the other value to this value. */
public operator fun plus(other: Short): Int
/** Adds the other value to this value. */
public operator fun plus(other: Int): Int
/** Adds the other value to this value. */
public operator fun plus(other: Long): Long
/** Adds the other value to this value. */
public operator fun plus(other: Float): Float
/** Adds the other value to this value. */
public operator fun plus(other: Double): Double
None 其中 return 一个 Byte
并且 plusAssign
没有重载,因此它们是隐式创建的。
首先它执行 plus(Byte)
returning Int
然后尝试赋值,但它需要一个 Byte
所以它会导致你的错误。