keepcalculator

06 Numbers and Powers

Unicode Sequence to Text: How to Encode

Card No. 06·12863 · Updated · Standard formula, shown below

The calculator on this card is switched off while it is checked. The formula is below, and the worked example shows how to do the sum by hand.

To encode a Unicode sequence into text, you convert each code point (like U+0041) into its binary representation and then into a specific encoding such as UTF-8, UTF-16, or UTF-32. The result is a byte sequence that software can decode back into the original characters, preserving all symbols, emojis, and scripts.

Formula

Unicode assigns each character a unique number called a code point, written as U+XXXX (hex). Encoding converts that number to bytes. The most common encoding, UTF-8, uses a variable number of bytes:

  • Code points U+0000 to U+007F (ASCII) → 1 byte (0xxxxxxx)
  • U+0080 to U+07FF → 2 bytes (110xxxxx 10xxxxxx)
  • U+0800 to U+FFFF → 3 bytes (1110xxxx 10xxxxxx 10xxxxxx)
  • U+10000 to U+10FFFF → 4 bytes (11110xxx 10xxxxxx 10xxxxxx 10xxxxxx)

The binary bits of the code point are split and placed into the x positions. For example, U+0041 (65 decimal) is below 128, so it becomes the single byte 0x41.

Worked Example

Take the code point U+1F600 (😀), decimal 128512. In binary it is 11111011000000000 (17 bits). UTF-8 needs 4 bytes: the pattern is 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx. Split the bits: 000 111110 110000 000000. Fill in: 11110000 10011111 10011000 10000000, which in hex is F0 9F 98 80. The calculator above, with default 65 (U+0041), shows the UTF-8 bytes as 41.

Why Encoding Matters

When you type text, the computer stores numbers, not letters. Unicode is the standard mapping of numbers to characters. Encoding defines how those numbers are serialized into bytes for storage or transmission. Without encoding, the same number could be interpreted differently, leading to mojibake (garbled text). For example, the byte 0xC3 0xA9 is “é� in UTF-8, but if decoded as Latin-1, it becomes “é�.

Choosing an Encoding

Your choice depends on your use case:

  • UTF-8 – Dominant on the web, backward-compatible with ASCII, efficient for English and most languages. Use for HTML, JSON, APIs.
  • UTF-16 – Common in Windows, Java, and JavaScript strings. Uses 2 or 4 bytes per character. Good when most characters are in the Basic Multilingual Plane.
  • UTF-32 – Fixed 4 bytes per character, simple but wasteful. Rarely used except for internal processing.
  • Legacy encodings – Like ISO-8859-1 or Windows-1252, only support limited character sets. Avoid for new work.

For most applications, UTF-8 is the safest default because it handles all Unicode characters and is widely supported.

Common Problems and Misconceptions

One frequent issue is mixing up code points and bytes. A character like “A� is U+0041, but its UTF-8 byte is also 0x41. For “é�, the code point is U+00E9, but the UTF-8 bytes are 0xC3 0xA9. People often think the hex value of the code point is the byte value, which is only true for ASCII.

Another misconception is that Unicode is an encoding. Unicode is the standard; UTF-8, UTF-16, and UTF-32 are encodings that implement it. Also, not all byte sequences are valid in a given encoding. For example, the byte 0xC0 is not allowed in UTF-8 because it would be an overlong encoding of a control character.

How to Encode in Practice

To encode a Unicode sequence to text in your own code, you typically use a function that takes a string and returns bytes. For example:

  • In Python: "Hello".encode('utf-8') returns b'Hello'.
  • In JavaScript: new TextEncoder().encode('Hello') returns a Uint8Array.
  • In Java: "Hello".getBytes(StandardCharsets.UTF_8).

You can also use online tools that let you paste a Unicode sequence and see its encoded bytes. The calculator above does the reverse: you enter a code point and see the bytes.

Safety and Accuracy

When encoding, always specify the encoding explicitly. Relying on system defaults can cause errors. For example, if you write a file without specifying UTF-8, it might be saved in the system’s default encoding, which could be Windows-1252 on some machines. This leads to data loss when characters outside that set are used.

Also, be careful with byte order marks (BOM). UTF-8 BOM (EF BB BF) is optional and can confuse some parsers. UTF-16 and UTF-32 require a BOM or an explicit endianness to be interpreted correctly.

Frequently Asked Questions

What is the difference between Unicode and UTF-8?

Unicode is a standard that assigns a unique number (code point) to every character. UTF-8 is an encoding that converts those numbers into a sequence of bytes. Unicode defines the alphabet, UTF-8 is one way to write it down.

How do I convert a Unicode code point to UTF-8 manually?

Write the code point in binary. If it fits in 7 bits, use one byte. If it needs 8-11 bits, use two bytes with the pattern 110xxxxx 10xxxxxx. For 12-16 bits, use three bytes with 1110xxxx 10xxxxxx 10xxxxxx. For 17-21 bits, use four bytes with 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx. Fill in the x’s with the bits of the code point, padding with zeros on the left.

Why does my text show as é instead of é?

This happens when a UTF-8 encoded file is decoded using a single-byte encoding like Latin-1. The bytes C3 A9 are interpreted as two separate characters “Ã� and “©�. To fix it, reopen the file with UTF-8 encoding.

Can I encode any Unicode character in UTF-8?

Yes, UTF-8 can represent every code point in the Unicode range U+0000 to U+10FFFF, which includes all characters, emojis, and special symbols. There are no unencodable characters in UTF-8.

What is the best encoding for a new project?

UTF-8 is generally the best choice for most projects because it is compact for ASCII text, supports all Unicode characters, and is the default for web technologies. Use UTF-16 only if you are working with a system that requires it, such as some Windows APIs.

Related

Formula used
in cp | Code point (decimal) | - | 65 | 0 | 1114111
out utf8 | UTF-8 bytes (hex) | - | if(cp

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →