将使用字节的Java函数转换为Kotlin [英] Convert java function which uses bytes to Kotlin
本文介绍了将使用字节的Java函数转换为Kotlin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将用Java编写的函数转换为Kotlin特有的问题。
I have a problem with converting function written in java to Kotlin specific.
这里是用Java编写的:
Here is written in Java:
private boolean isOldOemCommissioningFormat(byte[] assetData) {
if (assetData == null
|| assetData.length < mAssetDataDelimeterByteCount + mAssetDataOwnerIdByteCount + mAssetDataIdLeadingZerosByteCount + mAssetDataIdByteCount)
return false;
int oemMarkerIndex = mAssetDataDelimeterByteCount + mAssetDataIdLeadingZerosByteCount + mAssetDataIdByteCount;
if (assetData[oemMarkerIndex] == PARTIAL_OEM_MARKER || assetData[oemMarkerIndex] == FULL_OEM_MARKER)
return ((assetData[oemMarkerIndex + 1] >> 6) & 0x01) == 0;
return false;
}
但是,当我使用Android Studio IDE转换为Kotlin时转换器它给我的信息是:
However, when i am converting to Kotlin using Android Studio IDE converter it gives me this:
private fun isOldOemCommissioningFormat(assetData: ByteArray?): Boolean {
if (assetData == null || assetData.size < mAssetDataDelimeterByteCount + mAssetDataOwnerIdByteCount + mAssetDataIdLeadingZerosByteCount + mAssetDataIdByteCount)
return false
val oemMarkerIndex = mAssetDataDelimeterByteCount + mAssetDataIdLeadingZerosByteCount + mAssetDataIdByteCount
return if (assetData[oemMarkerIndex] == PARTIAL_OEM_MARKER || assetData[oemMarkerIndex] == FULL_OEM_MARKER) assetData[oemMarkerIndex + 1] shr 6 and 0x01 == 0 else false
}
我猜它给出了错误的转换,加上'shr'在红色处标记为未解决的引用。
It gives wrong convertion i guess, plus the 'shr' is marked on red as unresolved reference.
如何正确转换?
其他变量是:
public static final byte PARTIAL_OEM_MARKER = '#';
public static final byte FULL_OEM_MARKER = '&';
public static final int OEM_COMMISSIONING_CUSTOMER_ID_ENCODING_CHARACTERS_COUNT = 40;
public static final int OEM_COMMISSIONING_CUSTOMER_ID_ENCODING_FIRST_CHARACTER_INDEX = 64;
和
private final int mAssetDataIdLeadingZerosByteCount;
private final int mAssetDataIdByteCount;
private final int mAssetDataDelimeterByteCount;
private final int mAssetDataOwnerIdByteCount;
推荐答案
在Kotlin中, shr仅适用于Int和Long ,请尝试转换您的值
In Kotlin "shr" available only for Int and Long, try to convert your value
assetData[oemMarkerIndex + 1].toInt()
这篇关于将使用字节的Java函数转换为Kotlin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文