Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Byte Array to String in Kotlin

Tags:

android

kotlin

I'm trying to generate MD5 of a string in my android code using kotlin..

val md5 = MessageDigest.getInstance("MD5")
val hash = md5.digest(queryToSign.toByteArray(Charset.defaultCharset())).toString()

But this gives me:

[B@118072

Any thoughts?

like image 390
Audi Avatar asked Oct 06 '17 03:10

Audi


People also ask

How do you convert a byte array into a string?

There are two ways to convert byte array to String: By using String class constructor. By using UTF-8 encoding.

How do you convert bytes to strings?

One method is to create a string variable and then append the byte value to the string variable with the help of + operator. This will directly convert the byte value to a string and add it in the string variable. The simplest way to do so is using valueOf() method of String class in java.

How do you get bytes from string in Kotlin?

To convert a string to byte array in Kotlin, use String. toByteArray() method. String. toByteArray() method returns a Byte Array created using the characters of the calling string.

How do I make a byte array in Kotlin?

To create a Byte Array in Kotlin, use arrayOf() function. arrayOf() function creates an array of specified type and given elements.


1 Answers

Solved it.. Use BigInteger

val md5 = MessageDigest.getInstance("MD5")
val hash = BigInteger(1, md5.digest(queryToSign.toByteArray(Charset.defaultCharset()))).toString(16)
like image 109
Audi Avatar answered Oct 11 '22 02:10

Audi