I am trying to convert String
to ByteArray
in Kotlin.
我想在Kotlin中将String转换为ByteArray。
In Java we can convert using getBytes()
method.
在Java中,我们可以使用getBytes()方法进行转换。
4 个解决方案
#1
8
You do it like this:
你这样做:
val testVal = "Good Morning"
val bytes = testVal.toByteArray()
#2
5
Use String.toByteArray()
extension: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/to-byte-array.html
#3
1
Here is solution for this:
这是解决方案:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val checkString = "Good Morning"
val byteString = checkString.toByteArray()
}
}
#4
0
you can use .toByteArray() function on a string and the compiler will convert it
你可以在字符串上使用.toByteArray()函数,编译器会将其转换
#1
8
You do it like this:
你这样做:
val testVal = "Good Morning"
val bytes = testVal.toByteArray()
#2
5
Use String.toByteArray()
extension: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/to-byte-array.html
#3
1
Here is solution for this:
这是解决方案:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val checkString = "Good Morning"
val byteString = checkString.toByteArray()
}
}
#4
0
you can use .toByteArray() function on a string and the compiler will convert it
你可以在字符串上使用.toByteArray()函数,编译器会将其转换