Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

Why can text length differ between bytes, code points, and visible characters?

Quick answer

Text length depends on what you count. Bytes measure encoded storage, code points count Unicode values, and user-perceived characters approximate the units a person sees or edits. For e + a combining acute accent + 🙂, UTF-8 uses 7 bytes, there are 3 code points, and there are about 2 visible characters. State the encoding and counting method whenever a length limit matters.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I’m seeing different length numbers for the same text. What are bytes, code points, and visible characters actually counting?

Geez

They answer different questions:

  • Bytes count encoded data used to store or transmit text. The number depends on the encoding, so name it—for example, UTF-8.
  • Code points count Unicode values in the text. A letter and a combining accent attached to it are two code points, even if they appear together.
  • User-perceived characters are closer to the units a person treats as one character while reading or editing. A grapheme cluster can contain multiple code points. The count depends on the text and operation, so it is not one universal measure. [S1]

For a mixed example, take e followed by a combining acute accent, then 🙂:

  • UTF-8 bytes: 7 (1 + 2 + 4)
  • Unicode code points: 3
  • Approximate user-perceived characters: 2 (the accented letter and the emoji)

These are different measures, not conflicting answers.

Follow-up question

Which number should I use for a text-field limit or a storage check?

Geez

Match the count to what the limit is designed to control. If it limits encoded data size, count bytes using the specified encoding. If it limits Unicode values, count code points. If it is meant to limit the characters a person experiences, use a suitable user-perceived-unit counting method.

Check the limit’s documentation or ask its owner which measure it uses. Don’t assume an interface’s displayed count, a programming language’s string length, and a storage limit all use the same measure. If the method is unstated, report that ambiguity rather than presenting one count as definitive.

Follow-up question

I’m using JavaScript. Does string.length give me the visible-character count?

Geez

No. JavaScript strings are handled in UTF-16 code units, which differ from both code points and user-perceived characters. Some code points need more than one UTF-16 code unit, and a visible unit can consist of multiple Unicode values. [S2]

For e + combining acute accent + 🙂, JavaScript’s string.length is 4: one code unit for e, one for the accent, and two for the emoji. That is neither the 3 code points nor the approximate 2 user-perceived characters. Use the count your task requires; for a user-facing limit, choose a grapheme-aware approach that matches the intended behavior.

Follow-up question

Can you give me a reusable way to explain a count when I share it with someone?

Geez

Name the string, the measure, and—where relevant—the encoding and counting method. For example:

Text: e + combining acute accent + 🙂. UTF-8 byte count: 7. Unicode code-point count: 3. Approximate user-perceived character count: 2.

If you are reporting a programming-language property, name the language and property too. For instance, JavaScript string.length reports UTF-16 code units, not visible characters. [S2] This example does not establish what any particular app or service counts toward its limit; check that product’s stated rules.

A practical checklist

  • Identify whether the question concerns storage, Unicode values, or user-facing characters.

  • Name the encoding when reporting bytes, such as UTF-8.

  • Check whether a stated limit counts bytes, code points, grapheme clusters, or another measure.

  • For JavaScript, do not treat string.length as a visible-character count.

  • Show the exact sample text and label each reported count.

A few more useful answers

Can a visible character contain more than one code point?

Yes. A base letter followed by a combining mark can form one user-perceived unit while containing multiple code points. [S1]

Is a visible-character count always objectively fixed?

Not as a universal measure. User-perceived units depend on the text and operation, so specify the counting method when precision matters. [S1]

Does JavaScript `string.length` count UTF-8 bytes?

No. JavaScript string length is based on UTF-16 code units; it is not a UTF-8 byte count or a user-perceived-character count. [S2]

Sources

References used for this guide.

  1. [S1] W3C: Character encodings and user-perceived characters
    w3.org · Checked
  2. [S2] MDN: JavaScript strings, UTF-16 code units, code points, and grapheme clusters
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work