将使用字节的java函数转换为Kotlin

时间:2017-12-21 13:12:35

标签: java arrays kotlin

我将使用java编写的函数转换为特定于Kotlin的问题。

以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时,它给了我:

 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

}

我猜错了转换,加上&#39; shr&#39;在红色标记为未解决的参考。

如何正确转换?

其他变量是:

   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;

2 个答案:

答案 0 :(得分:1)

@ECHO OFF SET /P number=The number is: SET /a modulo=%number% %% 2 IF %modulo%==0 ( ECHO The number is even! ) ELSE ( ECHO The number is odd! ) 转换字节 到assetData[oemMarkerIndex + 1]Int

答案 1 :(得分:1)

在Kotlin&#34; shr&#34;仅适用于Int和Long,尝试转换您的值

assetData[oemMarkerIndex + 1].toInt()