我想在Kotlin中转换此方法,但我不知道要正确执行操作并具有正确的操作记忆的变量是什么:

public static UUID bytestoUUID(byte[] buf, int offset) {
    long lsb = 0;
    for (int i = 15; i >= 8; i--) {
        lsb = (lsb << 8) | (buf[i + offset] & 0xff);
    }

    long msb = 0;
    for (int i = 7; i >= 0; i--) {
        msb = (msb << 8) | (buf[i + offset] & 0xff);
    }

    return new UUID(msb, lsb);
}


你有正确的方法吗?谢谢

最佳答案

它应该是

import java.util.*
import kotlin.experimental.and

 fun bytestoUUID(buf: ByteArray, offset: Int): UUID {
    var lsb: Long = 0
    for (i in 15 downTo 8) {
        lsb = lsb shl 8 or ((buf[i + offset] and 0xff.toByte()).toLong())
    }
    var msb: Long = 0
    for (i in 7 downTo 0) {
        msb = msb shl 8 or ((buf[i + offset] and 0xff.toByte()).toLong())
    }
    return UUID(msb, lsb)
}

10-08 01:58