Skip to content

Charset

Detect text encodings, convert legacy bytes to and from UTF-8, and repair mojibake

Connector software
Free and open source
Service credentials
Not needed
Use with an agent

Install the Orchard skill in your project to help your agent find connectors, configure them, and verify SQL results.

npx skills add Query-farm/vgi-skills --skill orchard

Copy this request into your coding agent to get started.

Use https://query.farm/products/orchard/connectors/charset/index.md to help me query Charset with DuckDB or Haybarn 2.0.1 and VGI protocol 3.4. Ask only for missing details about the result I need or whether I want local execution, self-hosting or Query Farm hosting. Check the connector's actual hosting availability, credentials and input requirements. Read one format of each needed reference and the exact object declarations before writing SQL. Use a released connector, and distinguish a saved example result from anything you actually execute. If you cannot run the query, tell me what remains to verify.

See what you can query

Example output · captured
Connector release
v0.5.0
Engine
2.0.1
VGI protocol
3.4.0
WITH raw AS (SELECT id, charset.main.transcode(txt, enc) AS bytes FROM (VALUES (1, 'Grüße aus München', 'windows-1252'), (2, 'Привет, как дела?', 'windows-1251'), (3, 'こんにちは世界', 'shift_jis'), (4, 'naïve café', 'utf-8')) AS t(id, txt, enc)) SELECT id, charset.main.detect_encoding(bytes) AS detected, charset.main.detect_confidence(bytes) AS confidence, charset.main.is_valid_utf8(bytes) AS already_utf8, charset.main.to_utf8(bytes) AS text
FROM raw
ORDER BY id
Result
id detected confidence already_utf8 text
1 windows-1252 1 false Grüße aus München
2 windows-1251 1 false Привет, как дела?
3 Shift_JIS 1 false こんにちは世界
4 UTF-8 1 true naïve café

Quick start

Choose where to run Charset, then follow the steps below.

The connector runs alongside DuckDB or Haybarn. No Query Farm account is needed.

1. Install the vgi extension

Run this in your SQL client. On DuckDB, keep FROM query_farm on LOAD in later sessions.

setup.sql
SET allow_extension_repositories = 'allowed';

CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
    WITH PREFIX 'https://duckdb-extensions.query.farm';

INSTALL vgi FROM query_farm;
LOAD vgi FROM query_farm;

2. Attach the connector

Attach the release directly

Downloads the latest release for your platform and caches it locally.

ATTACH 'charset' AS charset (
  TYPE vgi,
  LOCATION 'github-auto://Query-farm/vgi-charset'
);

3. Run a query

query.sql
WITH raw AS (SELECT id, charset.main.transcode(txt, enc) AS bytes FROM (VALUES (1, 'Grüße aus München', 'windows-1252'), (2, 'Привет, как дела?', 'windows-1251'), (3, 'こんにちは世界', 'shift_jis'), (4, 'naïve café', 'utf-8')) AS t(id, txt, enc)) SELECT id, charset.main.detect_encoding(bytes) AS detected, charset.main.detect_confidence(bytes) AS confidence, charset.main.is_valid_utf8(bytes) AS already_utf8, charset.main.to_utf8(bytes) AS text
FROM raw
ORDER BY id
More example queries →
Technical details
Verified connector release
v0.5.0
Engine
2.0.1
VGI protocol
3.4.0
Verified on
December 1, 2026
Release notes on GitHub
Pin a version for production

When running the connector yourself, keep a specific release until you’re ready to upgrade. Replace the ATTACH statement above with this one and follow the same extension and credential setup.

Attach the release directly

Uses the verified release each time you attach the connector.

ATTACH 'charset' AS charset (
  TYPE vgi,
  LOCATION 'github-auto://Query-farm/[email protected]'
);

Overview

Character-encoding detection and UTF-8 transcoding (mojibake repair).

Catalog

Everything charset exposes once attached — read straight from the worker's released build.

charset 1 schema
main 1 table · 7 functions

Character-encoding detection and UTF-8 transcoding functions.

Schema examples (11)
Examples (11)

Triage a column of unlabelled bytes in mixed legacy encodings (produced here with transcode): detect each row's encoding and confidence, flag which rows are already UTF-8, and decode every row to UTF-8 text.

WITH raw AS (SELECT id, charset.main.transcode(txt, enc) AS bytes FROM (VALUES (1, 'Grüße aus München', 'windows-1252'), (2, 'Привет, как дела?', 'windows-1251'), (3, 'こんにちは世界', 'shift_jis'), (4, 'naïve café', 'utf-8')) AS t(id, txt, enc)) SELECT id, charset.main.detect_encoding(bytes) AS detected, charset.main.detect_confidence(bytes) AS confidence, charset.main.is_valid_utf8(bytes) AS already_utf8, charset.main.to_utf8(bytes) AS text
FROM raw
ORDER BY id;
Result
id detected confidence already_utf8 text
1 windows-1252 1 false Grüße aus München
2 windows-1251 1 false Привет, как дела?
3 Shift_JIS 1 false こんにちは世界
4 UTF-8 1 true naïve café

List the supported encodings that can store 'naïve café' losslessly: encode it into each label with transcode, decode it back with to_utf8_from, and keep the labels whose round trip is exact (the decode-only 'replacement' encoding has no encoder, so it is skipped).

SELECT label
FROM charset.main.supported_encodings
WHERE label <> 'replacement' AND charset.main.to_utf8_from(charset.main.transcode('naïve café', label), label) = 'naïve café'
ORDER BY label;

Before exporting names to a windows-1252 system, flag the ones that would not survive: encode each with transcode, decode the bytes back with to_utf8_from, and compare with the original.

SELECT name,
  charset.main.to_utf8_from(charset.main.transcode(name, 'windows-1252'), 'windows-1252') = name AS fits_1252
FROM (VALUES ('Café Müller'), ('Łódź'), ('東京'), ('Smith')) AS t(name)
ORDER BY name;

Reproduce double-encoding mojibake (UTF-8 bytes from transcode misread as windows-1252 by to_utf8_from) and show fix_mojibake restoring the original text.

SELECT original,
  charset.main.to_utf8_from(charset.main.transcode(original, 'utf-8'), 'windows-1252') AS garbled,
  charset.main.fix_mojibake(garbled) AS repaired
FROM (VALUES ('Café'), ('Ünïcödé'), ('“smart quotes” — dash')) AS t(original)
ORDER BY original;
7 more examples

Detect the character encoding of windows-1252 bytes for "café".

SELECT charset.main.detect_encoding('\x63\x61\x66\xE9'::BLOB);

Auto-detect and decode windows-1252 bytes to a UTF-8 string.

SELECT charset.main.to_utf8('\x63\x61\x66\xE9'::BLOB);

Decode Shift-JIS bytes to UTF-8 with an explicit codec label.

SELECT charset.main.to_utf8_from('\x93\xFA\x96\x7B'::BLOB, 'shift_jis');

Encode a UTF-8 string into windows-1252 bytes for export.

SELECT charset.main.transcode('café', 'windows-1252');

Repair double-encoded mojibake back to clean UTF-8.

SELECT charset.main.fix_mojibake('Café');

Check whether bytes are already valid UTF-8.

SELECT charset.main.is_valid_utf8('\x63\x61\x66\xC3\xA9'::BLOB);

List the first few supported encoding labels.

SELECT label FROM charset.main.supported_encodings() ORDER BY label LIMIT 5;
Tables
supported_encodings 12 examples1 col

Every encoding label the worker accepts (the encoding_rs / WHATWG set of canonical encoding names).

Columns
labelnot null VARCHAR
A canonical encoding label accepted by the worker, e.g. 'UTF-8', 'windows-1252', or 'Shift_JIS'. Valid as the encoding argument to to_utf8_from and transcode (except 'replacement', which is decode-only).
ConstraintsUNIQUE (label) · PRIMARY KEY (label)
Examples (12)

List the first few supported encoding labels.

SELECT label FROM charset.main.supported_encodings ORDER BY label LIMIT 5;

Count how many encoding labels the worker accepts.

SELECT count(*) AS n FROM charset.main.supported_encodings;

List the supported encodings that can store 'naïve café' losslessly: encode it into each label with transcode, decode it back with to_utf8_from, and keep the labels whose round trip is exact (the decode-only 'replacement' encoding has no encoder, so it is skipped).

SELECT label
FROM charset.main.supported_encodings
WHERE label <> 'replacement' AND charset.main.to_utf8_from(charset.main.transcode('naïve café', label), label) = 'naïve café'
ORDER BY label;
9 more examples
SELECT charset.main.detect_encoding('\x63\x61\x66\xE9'::BLOB) AS encoding;
SELECT charset.main.to_utf8('\x63\x61\x66\xE9'::BLOB) AS text;
SELECT charset.main.to_utf8_from('\x93\xFA\x96\x7B'::BLOB, 'shift_jis') AS text;
SELECT charset.main.transcode('café', 'windows-1252') AS bytes;
SELECT charset.main.fix_mojibake('Café') AS fixed;
SELECT charset.main.is_valid_utf8('\x63\x61\x66\xC3\xA9'::BLOB) AS ok;
SELECT label FROM charset.main.supported_encodings() ORDER BY label LIMIT 5;
WITH raw AS (SELECT id, charset.main.transcode(txt, enc) AS bytes FROM (VALUES (1, 'Grüße aus München', 'windows-1252'), (2, 'Привет, как дела?', 'windows-1251'), (3, 'こんにちは世界', 'shift_jis'), (4, 'naïve café', 'utf-8')) AS t(id, txt, enc)) SELECT id, charset.main.detect_encoding(bytes) AS detected, charset.main.detect_confidence(bytes) AS confidence, charset.main.is_valid_utf8(bytes) AS already_utf8, charset.main.to_utf8(bytes) AS text
FROM raw
ORDER BY id;
Result
id detected confidence already_utf8 text
1 windows-1252 1 false Grüße aus München
2 windows-1251 1 false Привет, как дела?
3 Shift_JIS 1 false こんにちは世界
4 UTF-8 1 true naïve café
SELECT original,
  charset.main.to_utf8_from(charset.main.transcode(original, 'utf-8'), 'windows-1252') AS garbled,
  charset.main.fix_mojibake(garbled) AS repaired
FROM (VALUES ('Café'), ('Ünïcödé'), ('“smart quotes” — dash')) AS t(original)
ORDER BY original;
Link
Scalar functions
detect_confidence (bytes BLOB) 1 example

A confidence proxy in [0,1] for the detected encoding, derived from whether the bytes decode losslessly (1.0) or required U+FFFD replacements (scaled down). NULL for empty/NULL input.

Arguments
bytes BLOB
The raw text bytes to score: confidence is derived from how cleanly these bytes decode under the detected encoding. NULL or empty input yields NULL.
ReturnsDOUBLE
Example

Score how confidently the bytes for "café" decode under the detected encoding (1.0 when lossless).

SELECT charset.main.detect_confidence('\x63\x61\x66\xE9'::BLOB);
Link
detect_encoding (bytes BLOB) 2 examples

Detect the character encoding of text bytes (BOM check, then the chardetng heuristic); returns the encoding label, e.g. 'UTF-8', 'windows-1252', 'Shift_JIS'. NULL for empty/NULL input.

Arguments
bytes BLOB
The raw text bytes whose character encoding should be detected; inspected as-is, with no decoding. NULL or empty input yields NULL.
ReturnsVARCHAR
Examples (2)

Detect the encoding of the bytes for "café" stored as windows-1252 (returns 'windows-1252').

SELECT charset.main.detect_encoding('\x63\x61\x66\xE9'::BLOB);

Triage a column of unlabelled bytes in mixed legacy encodings (produced here with transcode): detect each row's encoding and confidence, flag which rows are already UTF-8, and decode every row to UTF-8 text.

WITH raw AS (SELECT id, charset.main.transcode(txt, enc) AS bytes FROM (VALUES (1, 'Grüße aus München', 'windows-1252'), (2, 'Привет, как дела?', 'windows-1251'), (3, 'こんにちは世界', 'shift_jis'), (4, 'naïve café', 'utf-8')) AS t(id, txt, enc)) SELECT id, charset.main.detect_encoding(bytes) AS detected, charset.main.detect_confidence(bytes) AS confidence, charset.main.is_valid_utf8(bytes) AS already_utf8, charset.main.to_utf8(bytes) AS text
FROM raw
ORDER BY id;
Result
id detected confidence already_utf8 text
1 windows-1252 1 false Grüße aus München
2 windows-1251 1 false Привет, как дела?
3 Shift_JIS 1 false こんにちは世界
4 UTF-8 1 true naïve café
Link
fix_mojibake (text VARCHAR) 2 examples

Repair double-encoded mojibake (UTF-8 read as Latin-1/Windows-1252 then re-stored as UTF-8), e.g. 'Café' -> 'Café'. No-ops when it can't improve the text. NULL for NULL input.

Arguments
text VARCHAR
The possibly-mojibake UTF-8 string to repair, e.g. 'Café'. Returned unchanged when no improvement is possible; NULL input yields NULL.
ReturnsVARCHAR
Examples (2)

Repair classic double-encoded mojibake, turning 'Café' back into 'Café'.

SELECT charset.main.fix_mojibake('Café');

Reproduce double-encoding mojibake (UTF-8 bytes from transcode misread as windows-1252 by to_utf8_from) and show fix_mojibake restoring the original text.

SELECT original,
  charset.main.to_utf8_from(charset.main.transcode(original, 'utf-8'), 'windows-1252') AS garbled,
  charset.main.fix_mojibake(garbled) AS repaired
FROM (VALUES ('Café'), ('Ünïcödé'), ('“smart quotes” — dash')) AS t(original)
ORDER BY original;
Link
is_valid_utf8 (bytes BLOB) 1 example

Whether the bytes are already valid UTF-8. NULL for NULL input.

Arguments
bytes BLOB
The raw bytes to test for well-formed UTF-8. Checked as-is, with no decoding; empty input is valid UTF-8 and NULL input yields NULL.
ReturnsBOOLEAN
Example

Check whether a BLOB already holds valid UTF-8 (returns true).

SELECT charset.main.is_valid_utf8('\x63\x61\x66\xC3\xA9'::BLOB);
Link
to_utf8 (bytes BLOB) 1 example

Detect the encoding of text bytes and decode to a UTF-8 string (U+FFFD for undecodable bytes). NULL for empty/NULL input.

Arguments
bytes BLOB
The raw text bytes to decode. The encoding is auto-detected (BOM, then chardetng) and the bytes decoded to UTF-8; undecodable bytes become U+FFFD. NULL or empty input yields NULL.
ReturnsVARCHAR
Example

Auto-detect and decode windows-1252 bytes to the UTF-8 string "café".

SELECT charset.main.to_utf8('\x63\x61\x66\xE9'::BLOB);
Link
to_utf8_from (bytes BLOB, encoding VARCHAR) 1 example

Decode text bytes using an EXPLICIT encoding label (no detection), e.g. to_utf8_from(b, 'shift_jis'). ERROR if the label is unknown; NULL for NULL bytes.

Arguments
bytes BLOB
The raw text bytes to decode using the explicit encoding given in `encoding` (no auto-detection). Undecodable bytes within that encoding become U+FFFD. NULL input yields NULL.
encoding VARCHAR
The source encoding label to decode the bytes with — any canonical codec name the worker accepts, such as shift_jis. Call supported_encodings() for the full set of accepted labels. An unknown label raises an error.
ReturnsVARCHAR
Example

Decode Shift-JIS bytes to UTF-8 using an explicit codec label (returns "日本").

SELECT charset.main.to_utf8_from('\x93\xFA\x96\x7B'::BLOB, 'shift_jis');
Link
transcode (text VARCHAR, to_encoding VARCHAR) 2 examples

Encode a UTF-8 string into the named encoding's bytes (BLOB) for export, e.g. transcode('café', 'windows-1252'). Unmappable chars follow encoding_rs (HTML numeric refs for legacy codecs). ERROR on unknown label; NULL for NULL input.

Arguments
text VARCHAR
The UTF-8 string to encode into the target encoding's bytes. NULL input yields NULL.
to_encoding VARCHAR
The target encoding label to encode `text` into, e.g. 'windows-1252' or 'shift_jis' (see supported_encodings()). Characters the encoding cannot represent are emitted as HTML character references; an unknown label, or the decode-only 'replacement' encoding, raises an error.
ReturnsBLOB
Examples (2)

Encode a UTF-8 string into windows-1252 bytes for export to a legacy system.

SELECT charset.main.transcode('café', 'windows-1252');

Before exporting names to a windows-1252 system, flag the ones that would not survive: encode each with transcode, decode the bytes back with to_utf8_from, and compare with the original.

SELECT name,
  charset.main.to_utf8_from(charset.main.transcode(name, 'windows-1252'), 'windows-1252') = name AS fits_1252
FROM (VALUES ('Café Müller'), ('Łódź'), ('東京'), ('Smith')) AS t(name)
ORDER BY name;
Link

Try these queries

List the supported encodings that can store 'naïve café' losslessly: encode it into each label with transcode, decode it back with to_utf8_from, and keep the labels whose round trip is exact (the decode-only 'replacement' encoding has no encoder, so it is skipped).

SELECT label
FROM charset.main.supported_encodings
WHERE label <> 'replacement' AND charset.main.to_utf8_from(charset.main.transcode('naïve café', label), label) = 'naïve café'
ORDER BY label

Uses supported_encodingsto_utf8_fromtranscode

Before exporting names to a windows-1252 system, flag the ones that would not survive: encode each with transcode, decode the bytes back with to_utf8_from, and compare with the original.

SELECT name,
  charset.main.to_utf8_from(charset.main.transcode(name, 'windows-1252'), 'windows-1252') = name AS fits_1252
FROM (VALUES ('Café Müller'), ('Łódź'), ('東京'), ('Smith')) AS t(name)
ORDER BY name

Uses to_utf8_fromtranscode

Detect the character encoding of windows-1252 bytes for "café".

SELECT charset.main.detect_encoding('\x63\x61\x66\xE9'::BLOB);

Uses detect_encoding

Auto-detect and decode windows-1252 bytes to a UTF-8 string.

SELECT charset.main.to_utf8('\x63\x61\x66\xE9'::BLOB);

Uses to_utf8

Encode a UTF-8 string into windows-1252 bytes for export.

SELECT charset.main.transcode('café', 'windows-1252');

Uses transcode

Repair double-encoded mojibake back to clean UTF-8.

SELECT charset.main.fix_mojibake('Café');

Uses fix_mojibake

13 more examples in the Catalog, each listed under the table or function it uses.

Questions it can answer

Plain-English questions from the worker's agent test tasks. An AI agent with this catalog attached should answer each one with SQL.

  • What encoding does this worker detect for the UTF-8 byte representation of the text 'café'? Return only the single detected encoding label as one column.
  • What encoding does this worker detect for the windows-1252 byte representation of the text 'café'? Return only the single detected encoding label as one column.
  • Take the windows-1252 byte encoding of 'café' and decode it back into UTF-8 text.
  • Repair the double-encoded mojibake string 'Café' into correct UTF-8.
  • Treating the ASCII text 'hello' as raw bytes, does this worker consider them valid UTF-8? Return only the single boolean result as one column.
  • How many distinct character encodings does this worker support?

Charset is an independent open-source connector from Query.Farm. It is not affiliated with, endorsed by or sponsored by the services, projects or organisations it works with. Product names, logos and trademarks belong to their respective owners.