Code Point Index to UTF-8 Byte Offset

Convert zero-based Unicode scalar index to UTF-8 byte offset; reject unpaired surrogates and count scalars rather than grapheme clusters.

Description

Convert zero-based Unicode scalar index to UTF-8 byte offset; reject unpaired surrogates and count scalars rather than grapheme clusters.

Code-point indexes and UTF-8 byte offsets count different representations of text. Converting between them requires accounting for every preceding character's encoding length.1

When to use this tool

Convert zero-based Unicode scalar index to UTF-8 byte offset; reject unpaired surrogates and count scalars rather than grapheme clusters.

  • Prepare the supplied text, codePointIndex fields for this operation.
  • Compare the complete example with the representation used by your own input before carrying the result into another step.

How to use the tool and how it works

  1. Enter text, codePointIndex. Use the exact text syntax, units and index conventions explained below.
  2. Run the tool. If input validation fails, correct the supplied fields before interpreting any result.
  3. Read result.byteOffset. Interpret false, null and empty lists using the distinctions below; they can be valid outputs.

Reject unpaired surrogates, expand text into Unicode code points and require codePointIndex from zero through their count. Count UTF-8 bytes in the prefix before that index and return byteOffset.2

Input fieldMeaning and representationAccepted input
texttext.Text; at most 8,192 UTF-16 code units, subject to the grammar above
codePointIndexcode point index.Integer from 0 to a.length inclusive
Output fieldInterpretation
resultObject containing the named outputs below.
result.byteOffsetZero-based UTF-8 byte boundary offset in the encoded supplied text.
json
{
  "input": {
    "text": "A\ud83d\ude00B",
    "codePointIndex": 2
  },
  "output": {
    "result": {
      "byteOffset": 5
    }
  }
}

Limitations and assumptions

  • The index may identify the end boundary. Grapheme clusters can contain several code points, so this is not a user-perceived character or display-column conversion.
  • Text offsets and lengths use UTF-16 code units unless explicitly described as bytes or code points. CRLF, CR and LF are recognized as line endings; a terminal newline creates a final empty line in the shared line model.
  • Every declared top-level field is required and unexpected fields are rejected. Numeric inputs must be finite; ordinary text fields are bounded to 8,192 UTF-16 code units, with tighter operation-specific limits where stated.
  • Ordinary string lists have at most 128 entries of at most 128 code units each; numeric lists and matrices have separate bounded size checks. Nonfinite results and excessively large serialized outputs are rejected.
  • Indexes are zero-based and spans have an exclusive end unless explicitly stated otherwise. A valid false, null or empty-list result is distinct from malformed input.

Alternative or Complementary methods

Use a parser or language-aware editor when quoting, syntax or nested constructs matter. For plain text transforms, keep UTF-16 offsets, Unicode code points, grapheme clusters and UTF-8 byte offsets distinct; apply changes to the same source version used to calculate spans.

References

  1. RFC 3629: UTF-8 encoding — RFC Editor

  2. Tool behavior and example: Code Point Index to UTF-8 Byte Offset — tool.io

Similar or alternative tools

Don't forget to set a bookmark for tool.io!
Privacy | Imprint | Cookies