decodeFromHexString()
Anonymous contributor
Published Nov 17, 2023
Contribute to Docs
decodeFromHexString()
is a method in Kotlin designed to decode hex-encoded strings. Hexadecimal encoding is used for representing binary data in a human-readable format. This method simplifies the conversion of hex-encoded strings back into their original binary form. This method is a counterpart to encodeToHexString()
.
Syntax
fun decodeFromHexString(hexString: String): ByteArray
hexString
(Type: String): This is the input parameter for the function. It is the hexadecimal-encoded string that the function will decode.ByteArray
: This is the return of the function. ThedecodeFromHexString()
function returns an array of bytes (ByteArray).
Example
The example below decodes the encoded string 48656C6C6F2C20436F646541636164656D7921
and prints the decoded value.
import kotlinx.serialization.decodeFromHexStringimport kotlinx.serialization.encodeToHexStringimport kotlinx.serialization.Serializable@Serializabledata class HexString(val message: String)fun main() {val hexString = HexString("48656C6C6F2C20436F646541636164656D7921") // Hex-encoded stringval decodedData = decodeFromHexString(hexString)println(decodedData.message) // Output: Hello, CodeAcademy!}
Running this example produces the following result:
Hello, CodeAcademy!
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.