[英]How to convert a Byte to a Bitstring in Kotlin?
I have an ArrayList of Bytes.我有一个字节数组列表。 Firstly, when i print them i see integers?首先,当我打印它们时,我看到的是整数? And the second thing is, i want to convert each Byte to a Bitstring and add it to a new list of bitstrings.第二件事是,我想将每个字节转换为位串并将其添加到位串的新列表中。 How do i do that as there is no "i.toBitString"?由于没有“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?)}
}
}
preprocessing()预处理()
You can convert to any base with this method, in your case this should work:您可以使用此方法转换为任何基础,在您的情况下,这应该有效:
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) }
here is the docs:这是文档:
/**
* 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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.