android - 以 20 个 kotlin block 的形式迭代字符串的字符

标签 android for-loop kotlin iteration bluetooth-lowenergy

我正在尝试编写一个循环来分解字符串模板,并在 kotlin 中每次迭代向它发送 20 个字节。

fun verifySensor(template: String) {

    var previousValue = 0

    val iterationTimes = template.length / 20

    for (value in 1 until iterationTimes) {
        val templateByte = if (value == iterationTimes) {
            template.substring(previousValue until template.length - 1).toByteArray()
        } else {
            val subTemplate = template.substring(previousValue until (20 * value))
            subTemplate.toByteArray()
        }

        //Write the bytes to the sensor here
        writeToService(templateByte)
        previousValue += 20

        Log.i(TAG, "template >>> :: ${templateByte.toString(Charsets.UTF_8)}")


    }
    writeToService("VERIFY".toByteArray())


    Log.i(TAG, "Writing finger template in Byte")

}

我得到的输出是>>

040c62008efa8675463a f40785d3877b854870d8 b61b85d342f747ffd1f3 86648a877410fa2b887e 074b6ec8d16c887c9578 e6f8358586bac3f70bff 41a587c04af9d9ef394d 88132cebfe17d6c2881a c19979fefae2889102ca f3cf8ac48889c8f86b68 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 000000000000725a7353 95257462355224a396f2 0f000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000

但我期待>>

040c62008efa8675463a f40785d3877b854870d8 b61b85d342f747ffd1f3 86648a877410fa2b887e 074b6ec8d16c887c9578 e6f8358586bac3f70bff 41a587c04af9d9ef394d 88132cebfe17d6c2881a c19979fefae2889102ca f3cf8ac48889c8f86b68 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 000000000000725a7353 95257462355224a396f2 0f000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 000000000000733d

预期输出的最后两行被省略了

“00000000000000000000 000000000000733d"

最佳答案

据推测,您的代码中存在几个差一错误。

您可能会发现 chunked在这里有用的扩展函数:

fun verifySensor(template: String) {

    template.chunked(20).forEach { subTemplate ->
        writeToService(subTemplate.toByteArray())
    }

    writeToService("VERIFY".toByteArray())

}

请注意,如果您的模板字符串包含非 ascii 字符,则 20 个字符的编码子字符串可能会超过 20 个字节。如果这对您的服务来说是个问题,那么您应该先将字符串转换为字节,然后再以 block 的形式发送这些字节。

关于android - 以 20 个 kotlin block 的形式迭代字符串的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57384363/

相关文章:

Android ViewPager 在 TabLayout 之上显示 View

loops - 为什么在 Racket 代码中 for 循环如此缓慢

batch-file - 循环遍历批处理文件中的 ASCII 代码

android - 如何在 Kotlin 中设置默认的 BottomNavigationView 选项卡?

android - 在android中为选定的ListView Item设置背景颜色

android - 在 RecyclerView 中释放 ExoPlayer

android - Kotlin:在 fragment 内打开新 Activity

java - 不幸的是 MyApp 已停止。我该如何解决这个问题?

java - Android 中 RuntimeException ("Stub!"的含义

C++ For循环问题