Text encoding: the code page your legacy app never told you about

Most legacy migrations test logic and data volumes. Few test text. Then a customer named Muñoz or Zoë shows up in the new slice as Mu?oz, and nobody can say when it broke.

This note covers where the encoding assumptions hide and how to test for them before cutover.

Why it happens

The old stacks never said "UTF-8". They used whatever the machine used.

  • VB6 strings are UTF-16 in memory, but file I/O and declared API calls convert through the system ANSI code page. On a US machine that is usually Windows-1252.
  • Classic ASP uses Session.CodePage and Response.CodePage, defaulting to the server's ANSI page. Many apps never set either.
  • Access (Jet/ACE) stores text as Unicode compressed, but linked tables and exports often pass through ANSI.
  • FoxPro .dbf files carry a code page byte in the header. Many files have it set to 0, which means "unknown".
  • SQL Server varchar columns use the database collation's code page. nvarchar does not.

.NET 8 strings are UTF-16 and files default to UTF-8. Windows-1252 is not available by default in .NET 8. You must register it.

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var win1252 = Encoding.GetEncoding(1252);

Without that line, GetEncoding(1252) throws. Do not respond by switching to ASCII. That is how the question marks get in.

Step 1: Find every place text crosses a boundary

List each boundary. Do it by grep and by running the app, not by memory.

  1. Database columns: varchar and char versus nvarchar and nchar.
  2. Flat files the app reads or writes: CSV exports, fixed-width feeds, EDI, bank files.
  3. HTTP: Content-Type charset headers and <meta charset> tags on pages and form posts.
  4. COM and declared DLL calls with String parameters.
  5. Email bodies and attachments.

For Classic ASP, search for CodePage, Charset, and LCID. For VB6, search for StrConv, Open ... For Input, and Declare Function. For FoxPro, run DISPLAY STATUS or read byte 29 of each .dbf header.

Step 2: Measure what is actually stored

Do not trust the column type. Query for bytes outside plain ASCII.

SELECT TOP 100 CustomerId, Name,
       CAST(Name AS varbinary(200)) AS raw_bytes
FROM dbo.Customer
WHERE Name <> CAST(Name AS varchar(200)) COLLATE SQL_Latin1_General_CP1_CI_AS
   OR Name LIKE '%[^ -~]%' COLLATE Latin1_General_BIN;

Then count rows per suspicious byte. Typical findings are bytes 0x80 to 0x9F (smart quotes and the euro sign in 1252), and sequences like é, which mean UTF-8 was stored as if it were 1252. That second case is double encoding, and it is the hardest to undo. Count it separately.

Step 3: Pin the contract in the harness

Add characterization tests with deliberately awkward text. Use a short fixed set:

  • Accents: Muñoz, Zoë, São Paulo
  • Smart quotes and dashes: “quoted”, –
  • A character outside 1252, such as Ł, to see what the old app does with it
  • Leading and trailing spaces, since char(n) pads

Replay each through the old app and record what comes back. Record the failure too. If the old app turns Ł into ?, that is current behavior. Decide with the owner whether it stays a contract or becomes a fix. Do not decide it quietly in code.

Step 4: Choose the target on purpose

For new tables use nvarchar. For existing varchar columns, there are two honest options.

  • Keep varchar with a UTF-8 collation (_UTF8, SQL Server 2019 and later) or the existing collation, if the slice shares the table with the old app and the old app cannot read Unicode.
  • Convert to nvarchar once the old app no longer writes to the table, or behind a view.

While both apps run, the shared database must stay readable by the old one. This is the same constraint as in the strangler sequence generally: change the new code first, the shared schema last.

Collation also changes behavior. Sorting, = comparisons, and LIKE depend on it. Case and accent sensitivity differences can merge or split rows in a unique index. Check before you alter a column:

SELECT Name, COUNT(*)
FROM dbo.Customer
GROUP BY Name COLLATE Latin1_General_100_CI_AI
HAVING COUNT(*) > 1;

Any row returned is a duplicate under the new collation that was distinct under the old one.

Step 5: Convert at the edge, once

Pick one place where bytes become strings, and make it explicit. Read legacy files with Encoding.GetEncoding(1252). Write outbound files in whatever the receiving party specifies, and get that in writing. A bank or a trading partner's parser may still expect 1252 or even OEM 437. Inside the slice, never handle bytes.

For .dbf imports, read the header code page byte. If it is 0, sample the data and ask the people who typed it. A system used in a Mexican branch office may not be 1252 at all.

What we will not promise

We cannot always recover double-encoded data. If é is in the table, the original is usually inferable, but a bulk fix can damage rows that were correct. We repair those in a copy, diff the result, and have the business spot-check a sample before anything is written back.

Done when

  • Every text boundary is listed, with its encoding stated.
  • The harness contains the awkward-text set and passes against both apps.
  • No row in a migrated table differs after a round trip through the new code.
  • Shadow traffic shows no encoding-related diffs on real names and addresses.

This takes a day or two of measurement on most systems. Finding it after cutover takes much longer, because the first report is usually from a customer.