Hex

object Hex

Hexadecimal encode/decode. Pure Kotlin (no platform codecs), so it works in commonMain across every target. Port of libtorrent's aux::to_hex / aux::from_hex (hex.cpp).

Output is always lowercase, matching libtorrent. Decoding accepts either case.

Functions

Link copied to clipboard

Decode a hex string to bytes, throwing IllegalArgumentException if malformed.

Link copied to clipboard

Decode a hex string to bytes. Returns null if s has odd length or contains a non-hex character, mirroring libtorrent's from_hex returning false rather than throwing on malformed input.

Link copied to clipboard
fun encode(bytes: ByteArray, offset: Int = 0, length: Int = bytes.size - offset): String

Encode bytes (optionally a sub-range) as a lowercase hex string.