Java转Kotlin后实现代码报错

Raf*_*ães 3 android kotlin

在我来到这里之前,我已经尝试在StackoverFlow上找到这个问题。我正在尝试将Java类转换为Kotlin,Android Studio做得不太好。

我尝试手动完成但没有成功。

这是Java中的原始代码

    private static void appendHex(StringBuffer sb, byte b) {
        sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
    }
Run Code Online (Sandbox Code Playgroud)

这是Android Studio转换的代码

    private fun appendHex(sb: StringBuffer, b: Byte) {
        sb.append(HEX[b shr 4 and 0x0f]).append(HEX[b and 0x0f])
    }

Run Code Online (Sandbox Code Playgroud)

错误是在转换后,Android Studio无法识别shr& and,当我按ALT+ENTER 时,它会显示一个弹出窗口,Create extension function Byte.shr按 Enter 后,它创建了一个私人乐趣:

private infix fun Byte.shr(i: Int): Any {
    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
Run Code Online (Sandbox Code Playgroud)

相同,and但现在在弹出窗口中它有一个Import指向import kotlin.experimental.and或创建私人乐趣的选项:

private infix fun Any.and(i: Int): Int {
    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
Run Code Online (Sandbox Code Playgroud)

执行此操作并运行我的应用程序后,该类无法处理消息 An operation is not implemented: not implemented

如何实现这个工作?

for*_*pas 5

您可以在 Kotlin 中使用运算符(中缀函数)shrand类型Int(和Long)。
只是改变bb.toInt()

private fun appendHex(sb: StringBuffer, b: Byte) {
    sb.append(HEX[b.toInt() shr 4 and 0x0f]).append(HEX[b.toInt() and 0x0f])
}
Run Code Online (Sandbox Code Playgroud)