Convert a string to hex in Kotlin

A practical review of turning Kotlin strings into hexadecimal: what the conversion changes, when it is suitable, and how copy, search, and accessibility are affected.

Is converting a string to hex suitable?

Hex conversion is a bounded encoding step: it turns the string’s bytes into a compact alphabet of 0–9 and A–F (or a–f). The main benefit is an ASCII-safe, unambiguous dump of the exact byte sequence, which is useful for debugging, logging binary-adjacent data, comparing encodings, and handing values to APIs that expect hex. The decisive tradeoff is that the original wording, spacing, and script disappear from the stored text. Readers see digits, not the message.

Prefer the original plain string when people must read, edit, search, or quote the content. Prefer hex when the job is to preserve or inspect bytes, not meaning. Hex is a poor fit for display copy, captions, or any workflow where the transformed text is meant to stand in for the source. This review describes the language-level transformation itself. No specific Kotlin library, app, or online converter was supplied for evaluation, so claims below are about the encoding, not a named product.

What the transformation actually changes

This is a stored-character change, not a visual trick. Kotlin first encodes the string to bytes, commonly UTF-8 via toByteArray, then each byte becomes two hex digits. The result is a new string made only of hex characters (and, if you add them, separators such as spaces or dashes). Nothing is reversed, mirrored, or font-substituted. CSS, bidirectional display, and lookalike Unicode glyphs are not involved. The original code points are gone until someone decodes the hex back to bytes and interprets those bytes with the same charset.

Encoding choice is part of the meaning. The same Kotlin String can yield different hex if you use UTF-8, UTF-16, or a platform default. ASCII letters often look similar across UTF-8 and UTF-16-BE except for extra 00 bytes in UTF-16, but any non-ASCII character, combining mark, or multi-code-point emoji will differ. Hex encodes the bytes of those sequences; it does not reverse graphemes or walk visible characters. A family emoji or a letter-plus-accent is not one hex pair. It is as many pairs as there are bytes in the chosen encoding.

No illustrative input and output were supplied with this brief, so a worked example cannot be checked against a source sample. A typical pattern in Kotlin is: encode with an explicit charset, then format each byte as two digits. Kotlin’s standard library has also offered hex helpers in later releases; treat the exact function name and version as something to confirm in the SDK you ship, because this article does not verify a particular release.

fun String.toUtf8Hex(): String =
    toByteArray(Charsets.UTF_8)
        .joinToString(separator = "") { byte ->
            "%02x".format(byte)
        }

// Inverse (same charset required):
fun String.utf8HexToString(): String =
    chunked(2)
        .map { it.toInt(16).toByte() }
        .toByteArray()
        .toString(Charsets.UTF_8)
  • What changes in storage: each selected byte is replaced by two hex digits; length in characters typically doubles, plus any separators you insert.
  • What does not change: on-screen fonts, CSS styling, right-to-left layout, or mirrored glyphs. Those are presentation layers and are not this conversion.
  • Ambiguous words to avoid here: reverse, mirror, and font. Hex is encoding, not character-order reversal, not a mirrored typeface, and not a Unicode styling alphabet.
  • Round-trip rule: decoding must use the same charset and the same grouping (with or without separators, upper or lower case) that encoding used.

Copy, sharing, search, and accessibility

Hex of ASCII digits and letters copies and pastes as ordinary plain text in typical editors and messaging fields. That is the practical upside: the result stays in a tiny, widely supported character set. Case (ABCDEF versus abcdef) and optional separators are part of the payload. A destination that wraps lines, strips punctuation, or folds case can still leave the value decodable if you normalize before parse, but that behavior was not tested against a named app here.

Usability after sharing is limited. Search, diff, and in-document find look for hex digits, not the original words, so the source phrase will not match. Spellcheck and translation tools will treat the dump as noise. Assistive technology that reads the visible string will typically speak individual letters and numbers (for example “six one six five”) rather than the original sentence. No screen-reader or browser test is cited in the supplied material, so this is the expected character-level reading of hex, not a verified result in a particular voice engine.

Keep a plain-text original whenever a person must understand, edit, or quote the content. Use hex as a companion representation for bytes, logs, or interchange, and document the charset and digit case next to the value. If only the hex is stored, recovery depends on those parameters; they are not implied by the digits alone.

Practical limits to confirm in your own Kotlin code

Topic | What to pin down | Why it matters
Charset | UTF-8 unless you have a documented reason otherwise | Different encodings produce different hex for the same String
Case and width | Lowercase vs uppercase; always two digits per byte | Parsers and tests fail on mismatched format
Separators | None, spaces, or dashes | Sharing and line wrap can inject or drop them
Signed bytes | Format as unsigned 0–255 | Kotlin Byte is signed; naive string templates can print unwanted minus signs
Empty and odd length | Empty string → empty hex; odd-length hex is invalid on decode | Guards belong at the inverse, not only at encode
Binary vs text intent | Hex of a String is not a hash and not encryption | It hides readability; it does not add secrecy