UTF-8
Unicode Transformation Format - 8-bit
A Unicode encoding scheme. Variable length. The web standard character encoding. ASCII-compatible. Supports characters worldwide.
Unicode Transformation Format - 8-bit
A Unicode encoding scheme. Variable length. The web standard character encoding. ASCII-compatible. Supports characters worldwide.
UTF-8 (Unicode Transformation Format - 8-bit) is a method of encoding Unicode characters in a variable length of 1 to 4 bytes. Because it can represent every character in the world (Korean, Chinese characters, emoji, and more than 140,000 others) in a single encoding, it has become the standard for internationalization (i18n). ASCII characters are encoded in 1 byte, Korean characters in 3 bytes, and emoji in 4 bytes.
UTF-8 was designed in September 1992 by Ken Thompson and Rob Pike of Bell Labs. It was developed for the Plan 9 operating system at the time, with compatibility with existing systems as the top priority. Thompson is a co-creator of the C language and Unix, and Pike is known as a co-creator of the Go language. UTF-8, which these two masters designed in a single night, became the most successful character encoding in the history of computing.
ASCII compatibility is the key advantage of UTF-8. Since ASCII (0-127) is represented in UTF-8 as the same single byte, existing ASCII text files are recognized as UTF-8 without modification. In addition, the continuation bytes of a multibyte character always take the form 10xxxxxx, so character boundaries can easily be found in a byte stream, and synchronization is possible even when reading from the middle. Thanks to this self-synchronizing property, it is also resilient to data corruption.
As of 2026, more than 98% of the web uses UTF-8 (according to W3Techs). HTML5 recommends UTF-8 as the default encoding, and all major programming languages (Python 3, Go, Rust, JavaScript, etc.) have adopted UTF-8 as their default or recommended encoding. UTF-8 is also the de facto standard for development tools and data formats such as Git, JSON, and YAML.
UTF-8 uses 1 to 4 bytes depending on the code point range.
| Number of Bytes | Code Point Range | Byte Pattern | Examples |
|---|---|---|---|
| 1 byte | U+0000 ~ U+007F | 0xxxxxxx |
A (0x41) |
| 2 bytes | U+0080 ~ U+07FF | 110xxxxx 10xxxxxx |
e (0xC3 0xA9) |
| 3 bytes | U+0800 ~ U+FFFF | 1110xxxx 10xxxxxx 10xxxxxx |
a (0xEA 0xB0 0x80) |
| 4 bytes | U+10000 ~ U+10FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx |
😀 (0xF0 0x9F 0x98 0x80) |
# UTF-8 인코딩/디코딩 기초
text = "안녕하세요 Hello 🚀"
# 문자열 → UTF-8 바이트
encoded = text.encode('utf-8')
print(f"바이트: {encoded}")
# b'\xec\x95\x88\xeb\x85\x95\xed\x95\x98\xec\x84\xb8\xec\x9a\x94 Hello \xf0\x9f\x9a\x80'
# UTF-8 바이트 → 문자열
decoded = encoded.decode('utf-8')
print(f"문자열: {decoded}") # 안녕하세요 Hello 🚀
# ⚠️ 문자 길이 vs 바이트 길이 (핵심!)
print(f"문자 수: {len(text)}") # 14 (실제 글자 수)
print(f"바이트 수: {len(encoded)}") # 26 (저장 공간)
# 각 문자별 바이트 길이 확인
for char in "A가😀":
byte_len = len(char.encode('utf-8'))
print(f"'{char}' → {byte_len}바이트")
# 'A' → 1바이트 (ASCII)
# '가' → 3바이트 (한글)
# '😀' → 4바이트 (이모지)
# BOM 처리 (주의!)
with open("file.txt", "r", encoding="utf-8-sig") as f:
content = f.read() # BOM 자동 제거
# BOM 수동 제거
if content.startswith('\ufeff'):
content = content[1:]
# MySQL/MariaDB 한글 저장 시 주의
# utf8 (3바이트) 대신 utf8mb4 (4바이트) 사용!
# CREATE TABLE users (name VARCHAR(100)) CHARACTER SET utf8mb4;
// UTF-8 인코딩/디코딩 (Node.js & Browser)
const text = "안녕하세요 Hello 🚀";
// TextEncoder/TextDecoder (표준 Web API)
const encoder = new TextEncoder(); // 항상 UTF-8
const decoder = new TextDecoder('utf-8');
// 문자열 → UTF-8 바이트 (Uint8Array)
const encoded = encoder.encode(text);
console.log("바이트:", encoded);
// Uint8Array(26) [236, 149, 136, ...]
// UTF-8 바이트 → 문자열
const decoded = decoder.decode(encoded);
console.log("문자열:", decoded); // 안녕하세요 Hello 🚀
// ⚠️ 문자 길이 vs 바이트 길이
console.log(`문자 수: ${text.length}`); // 15 (JS는 UTF-16 기준!)
console.log(`실제 문자 수: ${[...text].length}`); // 14 (정확한 문자 수)
console.log(`바이트 수: ${encoded.length}`); // 26
// 이모지 주의! (Surrogate Pair)
const emoji = "😀";
console.log(emoji.length); // 2 (JS 내부는 UTF-16)
console.log([...emoji].length); // 1 (실제 문자 수)
// Buffer 활용 (Node.js)
const buf = Buffer.from(text, 'utf-8');
console.log(`바이트 길이: ${buf.length}`); // 26
console.log(`Buffer: ${buf.toString('hex')}`);
// 바이트 길이 계산 함수
function getByteLength(str) {
return new TextEncoder().encode(str).length;
}
console.log(getByteLength("가나다")); // 9 (3 x 3바이트)
// BOM 감지 및 제거
function removeBOM(str) {
return str.charCodeAt(0) === 0xFEFF ? str.slice(1) : str;
}
import java.nio.charset.StandardCharsets;
import java.io.*;
public class UTF8Example {
public static void main(String[] args) throws Exception {
String text = "안녕하세요 Hello 🚀";
// 문자열 → UTF-8 바이트
byte[] encoded = text.getBytes(StandardCharsets.UTF_8);
System.out.println("바이트 수: " + encoded.length); // 26
// UTF-8 바이트 → 문자열
String decoded = new String(encoded, StandardCharsets.UTF_8);
System.out.println("문자열: " + decoded);
// ⚠️ 문자 길이 vs 바이트 길이
System.out.println("length(): " + text.length()); // 15 (UTF-16 기준)
System.out.println("codePointCount: " +
text.codePointCount(0, text.length())); // 14 (실제 문자 수)
// 각 문자별 바이트 길이
for (String ch : new String[]{"A", "가", "😀"}) {
int byteLen = ch.getBytes(StandardCharsets.UTF_8).length;
System.out.println("'" + ch + "' → " + byteLen + "바이트");
}
// 파일 읽기/쓰기 (UTF-8 명시!)
try (BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream("test.txt"),
StandardCharsets.UTF_8))) {
writer.write(text);
}
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(
new FileInputStream("test.txt"),
StandardCharsets.UTF_8))) {
String line = reader.readLine();
System.out.println("읽은 내용: " + line);
}
// BOM 제거
String withBOM = "\uFEFF안녕하세요";
if (withBOM.charAt(0) == '\uFEFF') {
withBOM = withBOM.substring(1);
}
}
}
"The Korean text in the CSV file is garbled because of an encoding mismatch. CSVs saved from Excel default to EUC-KR, but our server reads them as UTF-8. Either change it to encoding='euc-kr' when opening the file, or ask them to re-save it from Excel as a UTF-8 CSV. Another option is to automatically detect the encoding with the chardet library."
"UTF-8 is variable-length, so ASCII takes 1 byte and Korean takes 3 bytes. UTF-16 uses 2 bytes for most characters, and only supplementary characters like emoji take 4 bytes (Surrogate Pair). If your text is mostly English, UTF-8 is more space-efficient, and if it is mostly Korean or Chinese characters, UTF-16 is more advantageous. However, because of web compatibility and ASCII compatibility, UTF-8 is the de facto standard. Java and JavaScript use UTF-16 internally, and because of this there is a pitfall where the length of an emoji comes out as 2."
"In MySQL, you should use utf8mb4 rather than utf8. The legacy utf8 supports only up to 3 bytes, so it cannot store emoji. utf8mb4 is the real UTF-8. Also, if you use VARCHAR(100) for a column, the limit may be based on bytes rather than 100 characters, so for columns that will hold Korean text, check with CHAR_LENGTH."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
The UTF-8 BOM (0xEF 0xBB 0xBF) is automatically added by Windows Notepad. It can cause JSON parsing errors and failure to recognize the first column of a CSV. In Python, use 'utf-8-sig', or remove it by directly checking for \ufeff. On Unix, UTF-8 without a BOM is the standard.
len("가나다") is 3, but its size in bytes is 9. DB VARCHAR, file size calculation, and network transfer volume are measured in bytes. Some API rate limits are also byte-based, so calculate Korean text as 3 times its length.
MySQL utf8 != real UTF-8. Use utf8mb4. For the collation, utf8mb4_unicode_ci (common) or utf8mb4_0900_ai_ci (MySQL 8.0+) is recommended. PostgreSQL uses UTF-8 by default, but LC_COLLATE needs to be checked.
1) Unify the entire project on UTF-8 (files, DB, API responses) 2) Specify Content-Type: text/html; charset=utf-8 3) Place <meta charset="UTF-8"> at the very top of the HTML 4) Use codePointCount/the spread operator when handling emoji