DEV Community

Justin Smith
Justin Smith

Posted on

Why a 140-character SMS can bill as 3 segments (with a JavaScript segment counter)

If you check an outbound SMS with message.length <= 160 before sending it through a carrier API, three things will eventually surprise your invoice:

  1. One non-GSM-7 character flips the whole message to UCS-2, dropping the single-segment limit from 160 septets to 70 UTF-16 code units (and 67 per segment once split).
  2. Ten ASCII-looking characters in the GSM-7 extension table (^, {, }, \, [, ~, ], |, €, and form feed \f) stay in GSM-7, but each one costs 2 septets instead of 1.
  3. Most emoji sit above U+FFFF, so a single emoji takes 2 UCS-2 code units (a UTF-16 surrogate pair) while switching the entire message to UCS-2.

Here is how the segment math works in 3GPP TS 23.038, along with a small JavaScript function that counts segments the way carrier gateways do.

The capacity rules

An SMS frame carries 140 bytes (1,120 bits) of payload:

  • GSM-7 (7 bits per septet): 1120 / 7 = 160 septets in a single SMS. When a message exceeds 160 septets, a 6-byte User Data Header (UDH) is prepended to each part so the handset can reassemble them, leaving 160 - ceil(48 / 7) = 153 septets per segment.
  • UCS-2 (16 bits per code unit): 1120 / 16 = 70 code units in a single SMS. With a 6-byte (48-bit) UDH, each concatenated segment holds (1120 - 48) / 16 = 67 code units.

The function

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

function countSmsSegments(text) {
  if (!text) return { encoding: "GSM-7", units: 0, segments: 0, perSegment: 160 };

  let isGsm7 = true;
  let extCount = 0;

  for (const ch of text) {
    if (GSM7_EXT.has(ch)) extCount++;
    else if (!GSM7_BASIC.has(ch)) {
      isGsm7 = false;
      break;
    }
  }

  if (isGsm7) {
    const units = text.length + extCount; // extension chars cost 2 septets (escape + char)
    const segments = units <= 160 ? 1 : Math.ceil(units / 153);
    return { encoding: "GSM-7", units, segments, perSegment: segments === 1 ? 160 : 153 };
  }

  // JavaScript strings are UTF-16, so text.length already counts UCS-2 code units
  // (1 for BMP characters, 2 for astral plane emoji / surrogate pairs).
  const units = text.length;
  const segments = units <= 70 ? 1 : Math.ceil(units / 67);
  return { encoding: "UCS-2", units, segments, perSegment: segments === 1 ? 70 : 67 };
}
Enter fullscreen mode Exit fullscreen mode

Four messages under 160 characters, three different bills

Take four appointment reminders that all look safe to a naive text.length <= 160 check:

const msgA = "Your appointment with Dr. Patel is confirmed for Tuesday at 2:30 PM. Reply YES to keep this slot or call 555-0199 to reschedule. Ref: #48210";
const msgB = "Your appointment with Dr. Patel is confirmed for Tuesday at 2:30 PM. Reply “YES” to keep this slot or call 555-0199 to reschedule. Ref: #482";
const msgC = "Your appointment with Dr. Patel is confirmed for Tuesday at 2:30 PM. Reply [YES] to keep this slot or call 555-0199 {desk} [ext 4] ~urgent~ [floor 2] €50.";
const msgD = "Your appointment with Dr. Patel is confirmed for Tuesday at 2:30 PM. Reply YES to keep this slot or call 555-0199 to reschedule. See you! 👋";

for (const [label, m] of [
  ["A: Straight quotes", msgA],
  ["B: Curly quotes (“YES”)", msgB],
  ["C: GSM-7 brackets/euro", msgC],
  ["D: Trailing emoji (👋)", msgD],
]) {
  console.log(label, "visibleChars:", [...m].length, countSmsSegments(m));
}
Enter fullscreen mode Exit fullscreen mode

Running that produces:

Message Visible chars ([...m].length) Encoding Billing units Per-segment limit Billed segments
A: Straight quotes ("YES") 140 GSM-7 140 septets 160 1
B: Smart quotes (“YES”) 140 UCS-2 140 code units 67 3
C: Brackets & Euro ([]{}~€) 154 GSM-7 165 septets 153 2
D: Trailing wave emoji (👋) 139 UCS-2 140 code units 67 3

Why each one behaves the way it does:

  • Message B (“YES”): Copy-pasting copy from Google Docs or Notion often introduces U+201C and U+201D. Neither is in GSM-7, so all 140 characters become UCS-2 (ceil(140 / 67) = 3 segments).
  • Message C ([YES] ... {desk} ... €50): Every character is valid GSM-7, and the string is only 154 characters long. But the 11 extension characters ([, ], {, }, ~, €) add 11 escape septets, pushing the payload to 154 + 11 = 165 septets—2 segments of 153 capacity.
  • Message D (👋): The message has 139 visible characters (138 ASCII characters + 1 emoji). Because 👋 (U+1F44B) is outside the Basic Multilingual Plane, it occupies 2 UTF-16 code units, making 138 + 2 = 140 UCS-2 units (ceil(140 / 67) = 3 segments).

Quick assertions for your test suite

import assert from "node:assert/strict";

assert.deepEqual(countSmsSegments(msgA), { encoding: "GSM-7", units: 140, segments: 1, perSegment: 160 });
assert.deepEqual(countSmsSegments(msgB), { encoding: "UCS-2", units: 140, segments: 3, perSegment: 67 });
assert.deepEqual(countSmsSegments(msgC), { encoding: "GSM-7", units: 165, segments: 2, perSegment: 153 });
assert.deepEqual(countSmsSegments(msgD), { encoding: "UCS-2", units: 140, segments: 3, perSegment: 67 });

// 35 astral-plane emoji = 70 UCS-2 code units (fits in 1 segment)
assert.equal(countSmsSegments("👋".repeat(35)).segments, 1);
// 36 astral-plane emoji = 72 UCS-2 code units (spills into 2 segments of 67)
assert.equal(countSmsSegments("👋".repeat(36)).segments, 2);
Enter fullscreen mode Exit fullscreen mode

Sanitizing smart punctuation before you send

If your templates don't intentionally need typographic punctuation, normalizing curly quotes and dashes before handing text to your SMS gateway keeps templates in GSM-7:

function normalizeForGsm7(text) {
  return text
    .replace(/[\u2018\u2019\u2032]/g, "'")
    .replace(/[\u201C\u201D\u2033]/g, '"')
    .replace(/[\u2013\u2014]/g, "-")
    .replace(/\u2026/g, "...");
}
Enter fullscreen mode Exit fullscreen mode

If you would rather not run code

We put the same detector into a free browser tool: paste a template, and it shows whether the string stays in GSM-7 or flips to UCS-2, highlights the exact character and Unicode code point (U+XXXX) that triggered UCS-2, counts 2-septet extension characters, and shows how many units remain in the current segment: SMS segment calculator.

The full breakdown of 3GPP TS 23.038 character tables, UDH headers, and carrier concatenation limits is here: SMS character limits, encoding, and segments explained. You can also browse the rest of our telecom and contact center calculators on the free tools page.

Disclosure: I work with the team at EaseDial, which publishes the calculator and guide linked above.

Top comments (0)