Junior — Middle
Can you explain how UTF-8 encoding works and its main advantages?
sobes.tech AI
Answer from AI
UTF-8 is a character encoding that uses a variable number of bytes (from 1 to 4) to encode each Unicode character. It is compatible with ASCII, as ASCII characters are encoded with a single byte with the same values.
Working principle:
- Characters with codes from 0 to 127 occupy 1 byte (matching ASCII).
- More complex characters occupy 2, 3, or 4 bytes, where the first bytes contain information about the length of the sequence, and the subsequent bytes contain the character data.
Main advantages of UTF-8:
- Compatibility with ASCII, which simplifies working with existing text.
- Efficient memory usage for Latin script texts.
- Universality — supports all Unicode characters.
- Self-synchronization — it is possible to determine the start of a character even if reading from the middle.
Example in Go for counting the number of characters in a UTF-8 string:
package main
import (
"fmt"
"unicode/utf8"
)
func main() {
s := "Привет, мир!"
fmt.Println("Number of characters:", utf8.RuneCountInString(s))
}