开发者社区> 问答> 正文

java翻译成kotlin的错误问题?报错

这是java代码

public static short byteToShort(byte[] b) {
        short s = 0;
        short s0 = (short) (b[0] & 0xff);// 最低位
        short s1 = (short) (b[1] & 0xff);
        s1 <<= 8;
        s = (short) (s0 | s1);
        return s;
    }

然后通过工具翻译成kotlin

fun byteToShort(b: ByteArray): Short {
    var s: Short = 0
    val s0 = (b[0].toInt() and 0xff).toShort()// 最低位
    var s1 = (b[1].toInt() and 0xff).toShort()
    s1 = s1 shl 8 ///这里报错了,请问怎么办?
    s = (s0 or s1).toShort()
    return s
}

展开
收起
爱吃鱼的程序员 2020-06-08 14:02:16 657 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    Short类型没有shl运算,改成Int,最后再cast到Short

    2020-06-08 14:02:31
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载