IDN Punycode ASCII And Unicode Domain Forms
Convert internationalized domain names between Unicode and ASCII Punycode via java.net.IDN, enforcing STD3 rules to reject unsafe host characters.
Code
import java.net.IDN;
public class PunycodeDomains {
public static void main(String[] args) {
String[] hosts = { "bücher.example", "例え.jp", "api.example.com" };
for (String host : hosts) {
// toASCII encodes non-ASCII labels into RFC 3490 Punycode (xn--...)
String ascii = IDN.toASCII(host, IDN.USE_STD3_ASCII_RULES);
String roundtrip = IDN.toUnicode(ascii);
System.out.println(host + " -> " + ascii + " -> " + roundtrip);
}
// Default mode permits underscores; USE_STD3_ASCII_RULES restricts to LDH host chars
String permissive = IDN.toASCII("bad_host.example");
System.out.println("permissive underscore: " + permissive);
try {
IDN.toASCII("bad_host.example", IDN.USE_STD3_ASCII_RULES);
} catch (IllegalArgumentException ex) {
System.out.println("STD3 rejected underscore: " + ex.getClass().getSimpleName());
}
}
}Step-by-step analysis — IDN Punycode ASCII And Unicode Domain Forms
IDN Punycode ASCII And Unicode Domain Forms reduces to the class PunycodeDomains and the function main.
for (String host : hosts) performs sequential per-record iteration neatly across entries.
Caveats
- Completely standalone — avoids non-bundled packages beyond core runtime standard modules.
Using it
Signature: public static void main(String[] args)
Logged lines display descriptive keys — permissive underscore:, STD3 rejected underscore: — when the file runs.
Key statements
String[] hosts = { "bücher.example", "例え.jp", "api.example.com" };
String ascii = IDN.toASCII(host, IDN.USE_STD3_ASCII_RULES);
IDN.toASCII("bad_host.example", IDN.USE_STD3_ASCII_RULES);
Interactive Tools for This Topic
JSON to Zod Schema ConverterInfer typed Zod z.object schemas from JSON payloads locally, supporting nested objects, merged array shapes, enums, nullable fields, and JSDoc comments.ASCII Table ReferenceLook up any ASCII code point 0-127 by decimal, hex, or character and see the binary, octal, and control-name view.Punycode / IDN ConverterConvert internationalized domain names between Unicode and RFC 3492 Punycode (xn--) form, label by label, with validation for malformed input.JSON Schema ValidatorValidate JSON data against a JSON Schema definition and get detailed error reports. Supports type checking, required fields, and common constraint validation.
FAQ
- What does IDN Punycode ASCII And Unicode Domain Forms print when run?
- Execute the whole file; each printed line is prefixed with a label like `permissive underscore:`, `STD3 rejected underscore:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.