Convert Byte Array to String in Kotlin

2019-04-09 13:50发布

问题:

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?

回答1:

Solved it.. Use BigInteger

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