String to Character Codes

Break a string into its characters with code points, UTF-8 bytes and HTML entities for each one.

4Graphemes
4Code points
5UTF-16 units

Everything you type here is processed on your own device. No input is sent to a server, logged, or stored.

What the String to Character Codes does

This string to character codes will break a string into its characters with code points, UTF-8 bytes and HTML entities for each one. Paste your text in, pick the options you need, and copy the result straight back out. It is most often used for identifying an unexpected character in a string. You may also want the subnet calculator.

How to use it

  1. Type or paste a few characters.
  2. Read the code point, bytes and HTML entity for each.
  3. Compare the four counts at the bottom against whatever limit you are working to.

How it works

There are four different ways to count a string and they give four different answers for anything beyond ASCII. A grapheme is what a reader perceives as one character. A code point is one Unicode scalar value. A UTF-16 code unit is what JavaScript's length property counts. A UTF-8 byte is what a database column limit usually measures.

The family emoji 👨‍👩‍👧 is one grapheme, five code points, eight UTF-16 code units and eighteen UTF-8 bytes. Every one of those numbers is correct for a different question, and using the wrong one is how a 200-character string gets rejected by a 255-byte column.

Code points above U+FFFF are stored in UTF-16 as a surrogate pair — two code units that are meaningless individually. Slicing a JavaScript string at an arbitrary index can split a pair and produce an invalid string, which is why Array.from and the spread operator are preferable to indexing for any text that might contain emoji.

When to use it

  • Identifying an unexpected character in a string.
  • Finding the code point for a character to use in an escape sequence.
  • Working out why a string exceeds a byte limit.
  • Debugging an encoding problem.

Common mistakes to avoid

  • Using string length as a character count for non-ASCII text.
  • Slicing a string at a UTF-16 index and splitting a surrogate pair.
  • Comparing a character count against a byte limit.

Frequently asked questions

Why does my emoji count as two characters?

Because JavaScript counts UTF-16 code units and most emoji need two. Some need more — a family emoji can be eight.

What is a surrogate pair?

Two UTF-16 code units that together encode one code point above U+FFFF. Splitting a string between them produces an invalid string.

Which count should I use for a database limit?

Usually UTF-8 bytes, since most column limits are measured in bytes. A 255-byte column holds far fewer than 255 characters of non-Latin text.

Related tools

See all developer tools →