Issue
I'm trying write a loop that will break down a template of strings and send it 20 bytes per iteration in kotlin.
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")
}
I'm getting an output of >>
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
But i'm expecting >>
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
The last two lines of the expected output is being omitted
"00000000000000000000 000000000000733d"
Solution
Presumably, there are a couple off-by-one errors in your code.
You may find chunked
extension function useful here:
fun verifySensor(template: String) {
template.chunked(20).forEach { subTemplate ->
writeToService(subTemplate.toByteArray())
}
writeToService("VERIFY".toByteArray())
}
Note that if your template string contains non-ascii characters, the encoded substring of 20 characters may be longer than 20 bytes. If that is a problem for your service, then you should convert string to bytes first, and send those bytes in chunks then.
Answered By - Ilya
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.