Kotlin Native how to convert ByteArray to String?
Asked Answered
E

5

31

I was playing with the kotlin-native samples. I wonder how I could get String from pinned ByteArray. Just want to print it in the console.

Ernestoernestus answered 24/3, 2018 at 17:35 Comment(1)
What is the encoding that was used to convert chars to bytes in that array?Koy
Y
12

It seems that this API has changed

Now just use this: string.toUtf8(start, end)

https://github.com/JetBrains/kotlin-native/commit/cba7319e982ed9ba2dceb517a481cb54ed1b9352#diff-45a5f8d37067266e27b76d1b68f01173

Legacy version:

Use stringFromUtf8

/**
 * Converts an UTF-8 array into a [String]. Replaces invalid input sequences with a default character.
 */
fun ByteArray.stringFromUtf8(start: Int = 0, size: Int = this.size) : String =
        stringFromUtf8Impl(start, size)

See here.

And if the byteArray is like CPointer<ByteVar> by interoperating C APIs, pleace use .toKString() in Kotlin-Native

Yen answered 24/3, 2018 at 18:29 Comment(4)
Thanks to @Willi Mentzel for adding the reference forstringFromUtf8's source code.Yen
Very thanks. But I can't find the source of stringFromUtf8 in the referenced link (here: StringBuilder.kt).Cloth
This API has been deprecated......ReplaceWith("string.toUtf8(start, end)" github.com/JetBrains/kotlin-native/commit/…Yen
The question was about converting ByteArray to String? How string.toUtf8(start, end) could help me?Politics
S
38

If you need a solution for the JVM, since stringFromUtf8 is only available for the native platform, use toString with a Charset as argument:

val byteArray = "Hello World".toByteArray(Charsets.UTF_8)   
val str = byteArray.toString(Charsets.UTF_8)

If you specifically only want to target native, use Sin's solution.

Sumerology answered 24/3, 2018 at 18:8 Comment(2)
This answer is for JVM and it is at the very top even though the question was about Kotlin/Native.Flameproof
@Flameproof it’s a glitch in the matrixSumerology
Y
12

It seems that this API has changed

Now just use this: string.toUtf8(start, end)

https://github.com/JetBrains/kotlin-native/commit/cba7319e982ed9ba2dceb517a481cb54ed1b9352#diff-45a5f8d37067266e27b76d1b68f01173

Legacy version:

Use stringFromUtf8

/**
 * Converts an UTF-8 array into a [String]. Replaces invalid input sequences with a default character.
 */
fun ByteArray.stringFromUtf8(start: Int = 0, size: Int = this.size) : String =
        stringFromUtf8Impl(start, size)

See here.

And if the byteArray is like CPointer<ByteVar> by interoperating C APIs, pleace use .toKString() in Kotlin-Native

Yen answered 24/3, 2018 at 18:29 Comment(4)
Thanks to @Willi Mentzel for adding the reference forstringFromUtf8's source code.Yen
Very thanks. But I can't find the source of stringFromUtf8 in the referenced link (here: StringBuilder.kt).Cloth
This API has been deprecated......ReplaceWith("string.toUtf8(start, end)" github.com/JetBrains/kotlin-native/commit/…Yen
The question was about converting ByteArray to String? How string.toUtf8(start, end) could help me?Politics
K
3

In my case this worked:

  1. ByteArray.decodeToString()
  2. ByteArray.toString(Charsets.UTF_8)
  3. ByteArray.commonToUtf8String()

They all gave the same result.

Kolomna answered 3/9, 2023 at 8:25 Comment(0)
R
-1

The OKIO library has a helper method for this commonToUtf8String

One can simply copy the method code don't need to add the entire lib just for this.

Reavis answered 13/9, 2022 at 14:12 Comment(0)
R
-1

Another solution that could be used everyone but especially makes sense looking for a Kotlin Multiplatform solution and using ktor library already is using io.ktor.utils.io.core.String function directly without adding extra third pary library or extra actual class implementation. For example:

Your build.gradle.kts for all platforms:

implementation("io.ktor:ktor-client-core:${Versions.ktor}")
implementation("io.ktor:ktor-client-android:${Versions.ktor}")
implementation("io.ktor:ktor-client-apache:${Versions.ktor}")
implementation("io.ktor:ktor-client-ios:${Versions.ktor}")

Then use it

io.ktor.utils.io.core.String(byteArray, offset, length, Charsets.UTF_8)
Rutilant answered 26/11, 2022 at 14:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.