I have an array of unsigned Integers (from 0 to 25...
# getting-started
d
I have an array of unsigned Integers (from 0 to 255)
Copy code
val signerPrivateKey : IntArray = intArrayOf(6,199,39,185,82,199,26,124,50,54,50,128,200,204,220,164,48,77,49,31,9,228,77,61,123,53,62,86,163,183,9,172,101,133,107,31,215,181,167,87)
how do I convert it to a ByteArray?
p
One way:
Copy code
val keyAsByte: ByteArray = ByteArray(signerPrivateKey.size) { signerPrivateKey[it].toByte() }
👍 1
f
Copy code
fun IntArray.toByteArray() : ByteArray {
    val data = mutableListOf<Byte>()
    this.forEach { 
        data.add(it.toByte())
    }
    return data.toByteArray()
}
d
great, thanks!
n
try something like byteArray.map{it.toByte()}
p
map
will give you a boxed `List<Byte>`; not critical for the case of a key but a huge performance downgrade compared to sticking with primitives
👀 1
e
do they even need to be in an IntArray in the first place? you can directly get a ByteArray with either of
Copy code
ubyteArrayOf(6u,199u,39u,185u,82u,199u,26u,124u,50u,54u,50u,128u,200u,204u,220u,164u,48u,77u,49u,31u,9u,228u,77u,61u,123u,53u,62u,86u,163u,183u,9u,172u,101u,133u,107u,31u,215u,181u,167u,87u).toByteArray()
byteArrayOf(6,199.toByte(),39,185.toByte(),82,199.toByte(),26,124,50,54,50,128.toByte(),200.toByte(),204.toByte(),220.toByte(),164.toByte(),48,77,49,31,9,228.toByte(),77,61,123,53,62,86,163.toByte(),183.toByte(),9,172.toByte(),101,133.toByte(),107,31,215.toByte(),181.toByte(),167.toByte(),87)
1