---
title: "SMS segments in fourteen languages: GSM-7, UCS-2 and the maths"
description: "A text message travels in segments, and one character can cut how much fits in each from 160 to 70. Bookatu writes its texts in fourteen languages, Czech and Japanese among them. Here is how we count segments the way the networks fill them, and how a text drops detail to fit the fewest segments it can."
url: "https://bookatu.com/engineering/sms-segments-in-fourteen-languages-gsm-7-ucs-2-and-the-maths"
language: "en"
section: "Engineering"
category: "Engineering"
author: "The Bookatu engineering team"
published: "2026-11-11"
updated: "2026-11-11"
tags: ["sms", "unicode", "i18n", "typescript", "engineering"]
---

# SMS segments in fourteen languages: GSM-7, UCS-2 and the maths

A text message travels in segments, and one character can cut how much fits in each from 160 to 70. Bookatu writes its texts in fourteen languages, Czech and Japanese among them. Here is how we count segments the way the networks fill them, and how a text drops detail to fit the fewest segments it can.

> **The short version**
>
> - GSM-7 fits 160 characters in one segment, and 153 in each part once a text is split. UCS-2 fits 70, and 67 per part.
> - One character outside the GSM-7 alphabet sends the whole text as UCS-2.
> - Nine GSM-7 characters, the euro sign among them, take two places. Most emoji take two UTF-16 code units. A network never cuts either across two parts, so dividing by the part size undercounts.
> - Before a text goes out, typographic dashes, curly quotes and unusual spaces become plain ones. Letters are never touched.
> - A text that runs long drops detail in a fixed order, and only to save a whole segment.

A booking text has a small job. It says who it is from and what was booked for when, and sometimes it carries a link. It also has a budget. Networks carry and charge for texts in segments, so a reminder that spills into a second segment costs twice as much as one that fits.

The words of our texts exist in fourteen languages, Czech and Japanese among them. The rules for what fits in a segment are the same in every one, and they are less obvious than they look.

## What a segment is

SMS was built around 140 bytes. With the GSM-7 alphabet, seven bits a character, that is 160 characters. A longer text is split, and each part carries a small header so the phone can join the parts back up. The header takes room, so each part holds 153 characters.

GSM-7 is a short alphabet. It has the Latin letters, digits, common punctuation and a handful of accented letters such as ä, ö, ü, ß, é, è, à, ñ, å, ø and æ. Any character outside it, a Czech ř, a Polish ł, any Japanese, a single emoji, sends the text as UCS-2 instead. That is 16 bits a character: 70 in one segment, 67 per part.

The switch applies to the whole text. One ł in an otherwise plain message turns 160 characters a segment into 70. A 160 character reminder that fit in one segment now takes three.

There is one more wrinkle. Nine characters reach GSM-7 only through an escape, from an extension table: € [ ] { } ^ ~ \ and |. Each takes two places.

## Counting it

```ts
// GSM 03.38, the basic set, and the extension table (two septets each).
const GSM7_BASIC = new Set([..."@£$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !\"#¤%&'()*+,-./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑÜ§¿abcdefghijklmnopqrstuvwxyzäöñüà"]);
const GSM7_EXTENDED = new Set([..."^{}\\[~]|€\f"]);

export function smsSegments(body: string) {
  const widths: number[] = [];
  let gsm = true;
  for (const ch of body) {
    if (GSM7_BASIC.has(ch)) widths.push(1);
    else if (GSM7_EXTENDED.has(ch)) widths.push(2);
    else { gsm = false; break; }
  }
  if (gsm) {
    const septets = widths.reduce((a, w) => a + w, 0);
    return { encoding: "gsm7", units: septets, segments: septets <= 160 ? 1 : packed(widths, 153) };
  }
  // UCS-2 counts UTF-16 code units: a character beyond them (most emoji) takes two.
  const units = body.length;
  return { encoding: "ucs2", units, segments: units <= 70 ? 1 : packed([...body].map((c) => c.length), 67) };
}
```

Two JavaScript details do the work here. for...of walks a string by code point, so an emoji is one step of the loop. body.length counts UTF-16 code units, so the same emoji counts as two. On the wire, texts written outside GSM-7 behave like UTF-16, so a character outside the basic plane takes two units of the 70.

## The off-by-one at the edge

Our first version divided: septets over 153, rounded up. It is the obvious formula, and it is wrong at the edges.

A network fills parts one character at a time, and it never cuts a character in two. If a part has one place left and the next character is a euro sign, which needs two, the euro sign starts the next part. The spare place goes unused.

Take 152 letters, a euro sign, then 152 more letters. That is 306 septets, which divides into two parts. On the network, the first part takes 152 letters and has one place left. The euro sign does not fit, so it starts part two. Part two fills to 153 with the euro sign and 151 letters, and the last letter goes into a third part. Three parts, not two.

UCS-2 has the same edge with emoji. 66 characters, an emoji, then 66 more is 134 units: two parts by division, three on the network. So the count now fills parts the way the network does:

```ts
function packed(widths: number[], perPart: number): number {
  let parts = 1;
  let used = 0;
  for (const w of widths) {
    if (used + w > perPart) {
      parts += 1;
      used = 0;
    }
    used += w;
  }
  return parts;
}
```

A text of one segment was always counted right, so this only changes split texts. It matters most when a text has to fit a set number of segments, which is where we found it.

## Keeping texts in GSM-7

Many characters that push a text into UCS-2 are not language at all. They are typography that slipped in on the way.

- French and Swedish write a negative UTC offset with the true minus sign, not a hyphen, so a time zone label can carry it.
- Some versions of ICU, the library behind date formatting, put a narrow no-break space inside a formatted time.
- Phone keyboards turn quotes into curly quotes, and two hyphens into a dash, inside business and service names.
- An ellipsis can arrive as one character instead of three dots.

Every text passes through one function before it is sent, which swaps these for plain ones:

```ts
export function smsSafe(body: string): string {
  return body
    .replace(/[\u2010-\u2015\u2212]/g, "-")             // dashes and the minus sign
    .replace(/[\u00A0\u2000-\u200A\u202F\u205F]/g, " ")  // unusual spaces
    .replace(/[\u2018\u2019\u201A\u201B\u2032]/g, "'")   // single quotes and the prime
    .replace(/[\u201C-\u201F\u2033\u00AB\u00BB]/g, '"')  // double quotes and guillemets
    .replace(/\u2026/g, "...");                         // the ellipsis
}
```

Letters are left alone. A Czech č or a Polish ł is the language, not typography. Those texts are UCS-2 whatever we do, and the rest of this post is about living with that.

## Choosing words that fit

Translation has a say too. Where a language has a GSM-7 word and another outside it for the same meaning, our texts use the GSM-7 one. In Portuguese, reserva rather than marcação. In French, stopper rather than arrêter. The meaning holds, and the text stays at 160 a segment.

Other languages have no such choice. Czech and Polish use letters outside GSM-7 in nearly every sentence. Japanese is outside it entirely. For those, 70 characters is the budget, and a long business name can use half of it.

## Trimming to the fewest segments

Some of our texts carry a link, so the client can open their booking from the message. A link cannot be shortened by translation, and in UCS-2 it takes a large share of 70 characters. For texts sent from a sender nobody can reply to, the link is the client's only way to act on the message. For those we went further: the text is fitted, not only written to fit.

Each such text has three versions, tried in order. The full version has the business, the service and the long date. The next drops the service. The last also shortens the date, to something like Wed 4 Nov. The first version with the fewest segments wins:

```ts
export function fitText(input: TextInput): FittedText {
  const { words, business, service, when, whenShort, url } = input;
  const versions = [
    { step: "full", body: fill(words.full, { business, service, when, url }) },
    { step: "no_service", body: fill(words.short, { business, when, url }) },
    { step: "short_date", body: fill(words.short, { business, when: whenShort, url }) },
  ];
  let best: FittedText | null = null;
  for (const v of versions) {
    const body = smsSafe(v.body);
    const length = smsSegments(body);
    if (!best || length.segments < best.segments) best = { body, step: v.step, ...length };
  }
  return best!;
}
```

The comparison is strict on purpose. A shorter version wins only by saving a whole segment, never a few characters, because characters inside a segment cost nothing. If the full text fits one segment, the client gets the full text.

The business name, the date and the link are never dropped. A text without the date is useless, and one without the link leaves the client no way to reach the booking from it. When even the shortest version is long, say a long business name in Japanese, the shortest version is the one used, in more segments.

## Testing every language

The tests fill each language's words the way the sender does. They use a deliberately long business name, a service with the person doing it, and a date in a month and on a weekday whose names are long in most languages. Then they run smsSafe and count segments. Each text must stay within a fixed number of segments, with one budget for GSM-7 and a larger one where a language needs UCS-2. A translation that grows past its budget fails the tests, in the language it grew in.

The counter has its own tests at the edges: 160 and 161 characters, 306 and 307, a euro sign as the 160th place, and the 152, euro, 152 case that started this.

## What we take from it

- Count segments by filling parts, not by dividing. A two-place character never splits.
- Clean up typography before sending. Never change letters.
- In translation, pick GSM-7 words where the language allows it.
- When trimming, drop detail in a fixed order, and only to save a whole segment.
- Test every language with the longest realistic inputs, not the example in the catalog.
