ARITHMETIC WORKSHEETS

Checking URL-component length after percent encoding

Percent encoding spells each escaped byte as three ASCII characters, so non-ASCII text grows by a factor of three per byte.

What a percent escape is

A URL may only carry a limited set of characters, so other bytes are written as a percent sign followed by two hexadecimal digits. RFC 3986, the URI standard, defines this scheme. The byte 0x20, a space, becomes %20. A byte that is not a letter, digit, hyphen, period, underscore or tilde is typically escaped when it appears in a component such as a query value. Each escape spends three ASCII characters on one byte, so the encoded form of any escaped text is three times as long as its byte count. This worksheet checks that arithmetic with two characters and a byte counter.

From characters to bytes first

Percent encoding works on bytes, not on characters, so text is first converted to bytes. This encoder uses UTF-8, the variable-length encoding in which ASCII characters take one byte and other characters take two to four. The letter e with an acute accent, é, is the code point U+00E9 and takes two bytes, 0xC3 and 0xA9. The step from characters to bytes is where the growth begins, because a single visible character can be several bytes, and every one of those bytes is then escaped separately.

The two-byte character é

The URL Encoder Decoder, set to encode the text é, returns %C3%A9. That is two escapes of three characters each, so six ASCII characters stand for one visible character. The UTF-8 Byte Length Calculator confirms the byte side: given the text %C3%A9 as plain text, it counts 6 bytes, since every character of the escaped spelling is ASCII and uses one byte. The number 6 is the length of the encoded text, which is not the same as the 2 bytes of the original character. Both counts are correct for their own text.

The four-byte emoji

The grinning face emoji is the code point U+1F600, which UTF-8 stores in four bytes: 0xF0, 0x9F, 0x98 and 0x80. Encoding it returns %F0%9F%98%80, twelve ASCII characters. One visible character has become twelve. In JavaScript, the same emoji has a string length of 2 because the language counts UTF-16 code units and the emoji needs a surrogate pair. So three different lengths describe one character: 1 code point, 2 UTF-16 units and 4 UTF-8 bytes, plus 12 characters once encoded.

Other characters and their sizes

The euro sign, U+20AC, takes three bytes in UTF-8, 0xE2, 0x82 and 0xAC, and encodes as %E2%82%AC, nine characters. The Chinese character 中, U+4E2D, takes three bytes as well and encodes as %E4%B8%AD. Most Chinese, Japanese and Korean characters take three bytes, so a short phrase in those scripts grows nine characters per visible character. A slash inside a query value encodes as %2F, and an ampersand as %26, which keeps them from being read as URL structure. These examples share one rule: the encoded length is three times the number of escaped bytes.

Mixed text: a partial expansion

Letters and digits in the unreserved set are left as they are, so mixed text grows only where it needs to. The word café encodes as caf%C3%A9. The first three letters stay as three characters and the last one becomes six, giving nine characters in all. The original text has five UTF-8 bytes, since é takes two, and the encoded text has nine. A string of 20 plain English letters stays at 20 characters, while a string of 20 accented characters may reach 120. The size change depends on the content, so you cannot predict the encoded length from the visible character count alone. A reliable way to budget is to count UTF-8 bytes of the text first, then add two characters for every byte that will be escaped. For café that is 5 bytes, with 2 of them escaped, so the encoded form has 5 plus 4, or 9, characters. The byte counter handles the first part of that calculation, and the multiplication by three for fully escaped text follows from it.

Source limits and encoded limits

A rule such as "the title may be at most 60 characters" and a rule such as "the URL may be at most 2,000 characters" apply to different things. The first counts the visible text, and the second counts the encoded form that actually travels. A title of 60 emoji, at 12 encoded characters each, would take 720 characters of a URL. Servers and browsers differ in what they accept for URL length. RFC 9110 recommends that senders and recipients support URIs of at least 8,000 octets, and many practical limits for older software and links shared in messages are much lower. This worksheet does not set a policy.

Which characters stay unescaped

RFC 3986 names a set of unreserved characters that never need escaping: the letters A to Z and a to z, the digits 0 to 9, and the four marks hyphen, period, underscore and tilde. Reserved characters such as the slash, question mark, number sign, ampersand and equals sign have roles in URL structure, so they are escaped when they appear as data inside a component. Everything outside printable ASCII is escaped byte by byte. That is why the accented e and the emoji above are escaped completely, and why the word café keeps its first three letters. The exact set a particular library leaves alone can differ at the margins, for instance for the apostrophe or the asterisk, so test the encoder you actually use.

Strict decoding and the scope of the check

The encoder described here follows the URI component convention, and the decoder is strict, which means it rejects malformed escapes and byte sequences that are not valid UTF-8. A lone %C3 with nothing after it is not valid text. These worksheet cases cover encoding of valid characters and a byte count. They do not tell you whether a particular server accepts a given URL, which characters your application should escape in a path or a query, or how a framework treats a plus sign. Check those rules in the documentation of the system that will receive the URL. Also remember that encoding twice turns each percent sign into %25, so %C3%A9 would become %25C3%25A9, which decodes back only to the first-level escape. Encode once, at the point where the component is inserted into the URL, and decode once on receipt.

Reproduce the checks

Two-byte character

Open the Url Encoder Decoder and enter:

Text or encoded input
é
Calculation mode
encode

Expected output:

Text

%C3%A9

Two escaped bytes use six ASCII positions.

Four-byte character

Open the Url Encoder Decoder and enter:

Text or encoded input
😀
Calculation mode
encode

Expected output:

Text

%F0%9F%98%80

Four escaped bytes use twelve positions.

Encoded text byte count

Open the Utf8 Byte Length Calculator and enter:

Text or encoded input
%C3%A9

Expected output:

Bytes

6

Every character of this escaped spelling is ASCII.

References and implementation scope

The examples describe the behavior of the linked Computandum tools. A successful local check does not establish that another application accepts the same format or applies the same limits.

Sources

Related tasks