我有一个字节的ArrayList。首先,当我打印它们时,我看到的是整数?第二件事是,我希望将每个Byte转换为Bitstring,并将其添加到新的位串列表中。如果没有"i.toBitString“,我该怎么做呢?
fun preprocessing() {
val userInput = readLine()
val charset = Charsets.UTF_8
val bytearray = userInput?.toByteArray()
var bitsets = ArrayList<BitSet>()
if (bytearray != null) {
// for(i in bytearray){
// bitsets.add(i.toBitset?)}
}
}预处理()
发布于 2020-09-07 04:49:53
您可以使用此方法转换为任何基,在您的情况下,这应该可以工作:
val userInput = "potatoes"
val bytearray = userInput.toByteArray(Charsets.UTF_8)
val bitsets = ArrayList<String>()
for (i in bytearray) {
bitsets.add(i.toString(2))
}
bitsets.forEach { println(it) }以下是文档:
/**
* Returns a string representation of this [Byte] value in the specified [radix].
*
* @throws IllegalArgumentException when [radix] is not a valid radix for number to string conversion.
*/
@SinceKotlin("1.1")
@kotlin.internal.InlineOnly
public actual inline fun Byte.toString(radix: Int): String = this.toInt().toString(checkRadix(radix))https://stackoverflow.com/questions/63767427
复制相似问题