Error Encyclopedia

UTF-8 Encoding / Decoding Error

Fix UTF-8 encoding errors like 'malformed UTF-8 data' or garbled text in your application.

What Does This Error Mean?

UTF-8 encoding errors occur when data is interpreted with a different encoding than it was written in, or when byte sequences do not form valid UTF-8 characters. This causes garbled text, database errors, and parsing failures.

Common Causes

1

Data was saved with one encoding (e.g., Latin-1) but read with another (UTF-8)

2

JSON or XML file saved with BOM or wrong encoding

3

Text input containing invalid UTF-8 byte sequences

4

Database table and connection using different character sets

5

File upload with encoding mismatch between form and server

6

API response specifying wrong charset in Content-Type header

How to Fix It

Specify encoding in Content-Type

Always specify the charset in HTTP headers and HTML meta tags.

// Server: set proper Content-Type charset
res.setHeader("Content-Type", "application/json; charset=utf-8")
res.setHeader("Content-Type", "text/html; charset=utf-8")

// HTML meta tag
<meta charset="UTF-8">

// HTML meta tag (alternative)
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

Fix database encoding

Ensure database, connection, and table all use UTF-8.

-- Check database encoding
SHOW CREATE DATABASE mydb;

-- Set database to UTF-8
ALTER DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- Set connection encoding
SET NAMES utf8mb4;

-- MySQL connection string
mysql://user:pass@host/db?charset=utf8mb4

Handle encoding conversion

Convert strings between encodings when needed.

// Node.js: convert Buffer encoding
const buf = Buffer.from("café", "latin1")
const utf8 = buf.toString("utf-8")

// Node.js: detect encoding with jschardet
const jschardet = require("jschardet")
const iconv = require("iconv-lite")

const buffer = fs.readFileSync("unknown.txt")
const detected = jschardet.detect(buffer)
const text = iconv.decode(buffer, detected.encoding)

Related Tools

Use these tools to debug and fix this error:

Related Errors

Other common errors in this category: