Skip to content

AdaIdna

Sanam edited this page Sep 23, 2026 · 1 revision

public static class AdaIdna

Converts domain names between Unicode and ASCII (Punycode), following UTS-46.

Method Returns
TryToAscii(ReadOnlySpan<byte> utf8Domain, Span<byte> destination, out int written) false if the domain cannot be converted or the buffer is too small
TryToUnicode(ReadOnlySpan<byte> utf8Domain, Span<byte> destination, out int written) Same
ToAscii(string domain) The ASCII form, or "" if it cannot be converted. Allocates.
ToUnicode(string domain) The Unicode form, or "" if it cannot be converted. Allocates.
AdaIdna.ToAscii("Bücher.example");            // xn--bcher-kva.example
AdaIdna.ToAscii("EXAMPLE.com");               // example.com
AdaIdna.ToUnicode("xn--bcher-kva.example");   // bücher.example

Span<byte> buffer = stackalloc byte[256];
AdaIdna.TryToAscii("Bücher.example"u8, buffer, out int written);

Not a validator

These methods map characters. They do not check that the result is a valid hostname. All of these convert "successfully" and come back unchanged:

AdaIdna.ToAscii("ex ample.com");   // ex ample.com
AdaIdna.ToAscii("a..b");           // a..b
AdaIdna.ToAscii("xn--");           // xn--

To validate, parse the URL. AdaUrl.TryGetHostname runs the full host parser, IDNA included, and fails on an invalid host:

AdaUrl.CanParse("https://ex ample.com/"u8);   // false

For an allow list, compare the output of AdaUrl.TryGetHostname against ASCII entries. Comparing Unicode forms can be fooled by lookalike characters.

Clone this wiki locally