user382591
user382591

Reputation: 1390

ANSI application and Vietnam Codepage


  1. I ve changed the codepage of my system to Russian as explained on this site
  2. PC is rebooted
  3. Then, I created a file in a dir with a name containing special russian character
  4. Then, I ve listed all files in this dir and tried to show the file with typical Delphi 7 code using:

 SearchRec: TSearchRec;

  FindFirst

  showmessage(SearchRec.Name);

  FindNext(SearchRec);
  FindClose(SearchRec);

The code works well.

When I redo all 4 steps with Vietnamese instead of Russian, the filename shown with showmessage is not correct. Some ? appear instead (see the screenshot):

enter image description here

Please help

Upvotes: 0

Views: 1144

Answers (1)

Arnaud Bouchez
Arnaud Bouchez

Reputation: 43033

This is due to the way Delphi versions prior to 2009 implements their string type. It is not a problem of font, but a problem of character encoding.

All string variables, and also all Windows API calls are performed using ANSI encoding. With ANSI, you can use only one code page at a time. In order to mix code pages (i.e. mix russian and vietnamese encodings), you'll need to process the text and call UNICODE Windows API.

Here is what occurred in your case:

  • You create a file with russian characters by Windows, using UNICODE encoding;
  • When you read the file using vietnamese current code page, only the first 127 characters (i.e. ASCII seven chars, e.g. numbers, main punctuations and English letters), are able to be read from UNICODE into ANSI vietnamese: during the conversion, all uncorrect characters are converted as ? in your ANSI vietnamese string.

So you have several workarounds:

  • Upgrade to Delphi >= 2009, and your string will be UNICODE, so you will be able to mix character sets;
  • Use widestring for storing your text, and call directly the windows wide APIs - that is, you can't use the VCL units nor FindFirst/FindNext as defined in SysUtils, nor ShowMessage as defined in Dialogs.

Of course, the first one is the easiest!

Upvotes: 1

Related Questions