Recursive AI

Normalize a Unicode domain in C#

OneContact is a free, open-source contact data library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies, no network access.

Get the ASCII host, including subdomains.

Terminal
dotnet add package RecursiveAI.OneContact

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneContact;
 
var url = WebAddress.Parse(
    "www.Bücher.example");
Console.WriteLine(url.Host);

The ASCII host is:

Text
www.xn--bcher-kva.example

The xn-- prefix marks a label encoded with Punycode.

Unzip the example, then dotnet run.

Full program

C#
using RecursiveAI.OneContact;
 
var composed = WebAddress.Parse("www.Bücher.example");
var decomposed = WebAddress.Parse("www.Bu\u0308cher.example");
Console.WriteLine(composed.Host);
Console.WriteLine(decomposed.Host);

dotnet run prints:

Text
www.xn--bcher-kva.example
www.xn--bcher-kva.example

The first input uses ü; the second uses u followed by a combining diaeresis (\u0308). Parse throws ContactParseException if the web address can’t be parsed.

Variations

Read the normalized host from a full HTTPS URL with a path:

C#
using RecursiveAI.OneContact;
 
var url = WebAddress.Parse(
    "https://faß.example/catalog");
Console.WriteLine(url.Host);
Text
xn--fa-hia.example

WebAddress doesn’t expose a Unicode host; for a domain in an email address, use EmailAddress.UnicodeDomain (with the same using RecursiveAI.OneContact;):

C#
Console.WriteLine(EmailAddress.Parse("info@xn--bcher-kva.example").UnicodeDomain);
Text
bücher.example

The short answer

Read WebAddress.Host to get the full normalized host. OneContact assumes https for a bare domain such as www.Bücher.example. It returns www.xn--bcher-kva.example as the host, without the scheme, port, or path. Use Host, not RegistrableDomain: the latter uses the Public Suffix List to look up the domain that can be registered, which can leave out subdomains such as www.

What does “normalize Unicode” mean for a domain name?

Unicode can represent the same letter in more than one way. The two spellings of ü in the full program differ as strings but give the same host. Ordinary Unicode normalization can put both spellings into the same Unicode form, but it does not turn a hostname into ASCII.

For web hosts, OneContact follows Unicode UTS #46: it maps characters, applies Unicode normalization, checks the labels between the dots, and encodes non-ASCII labels. The URL path isn’t part of this operation. WebAddress parses URLs rather than validating a domain-only form field, and does not check DNS. If your field must accept only domains, don’t infer that from parse success: the same API also accepts full URLs and IP addresses.

What is Punycode in a domain name?

Punycode is the encoding used for domain labels that contain non-ASCII characters. IDNA, short for Internationalized Domain Names in Applications, marks an encoded label with xn--. In www.xn--bcher-kva.example, only bücher had to be encoded; the ASCII labels www and example did not. The faß example also keeps ß in the encoded result rather than changing it to ss, because OneContact’s web host processing uses non-transitional mapping.

All onecontact recipes