以 20 个 kotlin 块的形式迭代字符串的字符

iterate over characters of string in chunks of 20 kotlin

我正在尝试编写一个循环来分解字符串模板,并在 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 个字节。如果这对您的服务来说是个问题,那么您应该先将字符串转换为字节,然后再以块的形式发送这些字节。

我觉得可能是错的val iterationTimes = template.length / 20。例如;

20 / 21 returns 1 但是你应该迭代 2 次。所以也许你可以像 val iterationTimes = template.length / 20 + 1

这样增加 iterationTimes

第一,本次划分:

val iterationTimes = template.length / 20

是一个整数除法,所以如果字符串的长度不是20的倍数那么结果将是除法的整数部分(对于length = 90结果是4)并且计算的迭代将忽略字符串的最后部分(除法的剩余部分 length / 20)。

其次,当你使用until定义范围时,像这样循环:

for (value in 1 until iterationTimes)

你应该知道不包括上限。
因此,当 value 达到 iterationTimes - 1 并且您错过了最后一次迭代时,迭代停止。

所以这对我有用

fun verifySensor(template: String) {

    val raw = template.decodeHex().toByteArray()


    var index = 0
    while (index < raw.size) {
        val chunkSize: IntRange = if (index + 19 > raw.lastIndex) {
            IntRange(index, raw.lastIndex)
        } else {
            IntRange(index, index + 19)
        }

        val chunkList = raw.slice(chunkSize)

        val chunk = chunkList.toByteArray()
        Thread.sleep(350)
        index += 20

        writeToService(chunk)
    }
    Thread.sleep(350)
    writeToService("VERIFY".toByteArray())
}

这里::

val raw = template.decodeHex().toByteArray()

我使用 Okio 库 https://github.com/square/okio 来解码十六进制字符串

implementation "com.squareup.okio:okio:2.3.0"

我在尝试发送字节块时遇到问题,大部分字节没有发送到服务。 因此,我必须让线程休眠 350 毫秒,以便每个字节块完全发送。