12

How to make a Unicode program in Delphi 2010?

I have English Windows and "Current language for non-Unicode programs" is English too. Static controls look good but if I try to change them (Label.Caption := 'unicode value' or Memo.LoadFromFile(textFilename) ) the text looks like: $^$&%*(#&#.

How to fix it?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Michael
  • 475
  • 2
  • 9
  • 17
  • 3
    -1. All programs are Unicode programs in Delphi 2010. If you're getting garbage, then there's more to your problem that you need to describe here. You need to describe what you've done, what happens, and what you expected to happen instead. Please clarify. Post some code. Post some pictures, if you need to. – Rob Kennedy Jun 07 '11 at 19:49
  • All Delphi 2010 applications are Unicode. In addition, all versions of Windows in which Delphi 2010 applications can run (I think -- at least all non-ancient versions of Windows) are Unicode. You are doing something very wrong. Please give us a minimal project displaying the issues you are observing. – Andreas Rejbrand Jun 07 '11 at 19:50
  • 12
    Easy, guys. This is a new user. He doesn't need to be downvoted and close-voted for a question like this; he needs to edit the question and give a more clear answer. – Mason Wheeler Jun 07 '11 at 20:26
  • @Mason: It isn't that bad. No one can answer the question as it stands, and the OP can still edit it. If (s)he does, it won't be long before it is opened again. – Andreas Rejbrand Jun 07 '11 at 20:27
  • 2
    I found an issue. The first project I have created with other Windows language settings, then I have changed settings in Windows. Delphi has saved all a project text not in UTF-8. Why! I just have saved the project and reopened it and I have seen that all characters has been changed. Why does Unicode Delphi save not in UTF-8???... – Michael Jun 07 '11 at 20:35
  • 3
    @Mason, this question *should* be down-voted. It's a lousy, unanswerable question. If Michael edits it to make it a good question, I'll re-vote. I'll also vote to re-open it, but again, only *after* it's a decent question. – Rob Kennedy Jun 07 '11 at 20:42
  • Michael, if you'd like to ask about why Delphi saves files with a particular encoding, please post a new question about it. I don't think it's related to the issue you have in *this* question, but I'll again ask you to please *edit* this question to clarify exactly what problem you're having. – Rob Kennedy Jun 07 '11 at 20:44
  • 13
    @Rob: I disagree. You've been around here for years. You can't hold a newbie to the same standards of quality that you aspire to in your own writing, and slap all sorts of negative effects on their posts if they don't measure up. That creates a hostile community that new people don't want to come participate in. – Mason Wheeler Jun 07 '11 at 20:46
  • 8
    StackOverflow needs a way to downvote a downvote. Come on guys, this takes "heavy handed" to a new level, and not in a good way. Thank the gods I didn't have "helpful" people like you around when I was finding my way in Delphi. – Deltics Jun 07 '11 at 21:16
  • Thank everybody for helping. As I sad, Delphi 2010 has saved the project not in UTF-8, I have converted to UTF-8 and all have started to work! – Michael Jun 07 '11 at 21:37
  • 2
    Yes, @Deltics, perhaps there does need to be some other way of showing disagreement with a down-vote. As it stands now, it looks like three people actually think this is a clear, useful question that shows research effort, which I really don't see. However, I think the comment I gave when I voted on this question was helpful because I explained its shortcomings, but it now occurs to me that I could have been even more helpful by also reminding Michael that he could edit the question and by mentioning that improving the question could change my vote. – Rob Kennedy Jun 07 '11 at 22:41
  • I changed the title of the question and fixed the grammar. – Warren P Jun 08 '11 at 07:09

3 Answers3

19

Welcome to StackOverflow. Please post your code when you have such a problem. I will explain the most likely sources of the problem like the one you are seeing, but I can't help you fix it if you don't post your code. Also I have to make a lot of assumptions because you've asked me to guess almost everything about your question, which is why it got closed. I hope you give more detail in the future, and we can avoid closed questions.

Let me assume a bunch of things because you haven't given me very much data to go on.

  1. You have used Delphi before, and you know about the fundamental type names like String, Char, and so on.

  2. You may not be aware of the Unicode differences between Delphi 2007 (char=Ansichar/string=Ansistring) and Delphi 2009-or-later (including Delphi 2010 and XE) where Char=UnicodeChar, and String=UnicodeString.

  3. The most common reason you would see garbage (represented in your question as the text looks like $^$&%*(#&#.") is if you have tried to directly manipulate byte-size AnsiCharacter data and coerce it in a wrong way into a UnicodeString.

  4. MJN also noticed, from one of your comments that you are also having trouble with source code that contains unicode characters that was not saved as a UTF8 file... When I try to put Unicode characters into a source file Delphi automatically asks me this question, which I assume you also see, and answer correctly (the correct answer is yes)... But your question doesn't mention this at all, you really should try to update your question to specify the source of your problem.

enter image description here

Here is the right-click file format menu, from which you can change the encoding at any time, the recommended value is UTF8 as shown here:

enter image description here

You should definitely post the affected code that generates the incorrect string values. You should start, not with a giant application that you are trying to port to Unicode Delphi (which is the fourth and largest assumption I'm making here) and rather, start with some small sample code.

here's an example of "badly written code", that happens to still work in Delphi 7, because each character is one byte in size, but this assumption does not travel upwards to 2009 and XE Delphi:

procedure Tform1.TestBad;
var
 x:PAnsiChar;
 s:String;
begin
  x := 'test';
  s := Copy(PChar(x),1,10);
  Self.Caption := s;
end;

here's the same contrived sample code "fixed" (more like not-intentionally broken) so it will at least work in delphi XE:

procedure Tform1.TestLessBad;
var
 x:PAnsiChar;
 s:String;
begin
  x := 'test';
  s := Copy(x,1,10);
  Self.Caption := s;
end;

The use of pointers above is contrived, and unnecessary, except that I am trying to teach with this example.

The first example will create unicode chinese characters in the caption of the form instead of showing the text 'test', because 2 bytes have become a single character because I have intentionally done something BAD to show you one easy way to generate this noise you speak of, by making mistakes in my code.

If you are having trouble with particular unicode code-points, let me suggest you try this notation:

c := Char($21CC);  // this is U+21CC (cool two arrows thingy used in chemistry to indicate a reversible reaction)

Alternatively you will see this, which is almost the same thing:

c := #$21CC; // U+21CC

Notice how you don't need a UTF8 encoded file to store things you write this way.

Warren P
  • 65,725
  • 40
  • 181
  • 316
7

The comments

Why does Unicode Delphi save not in UTF-8?

and

Delphi 2010 has saved the project not in UTF-8, I have converted to UTF-8 and all have started to work

seem to be related to the encoding of the Delphi project (dpr and/or pas) source code files. If they are set to ANSI for example, characters which do not belong to the current code page might be saved incorrectly.

The IDE (in Delphi 2009 at least) creates new units with file format set to ANSI. For mixed-language source code, UTF-8 (or one of the UCS flavours) has to be activated manually, using the context menu in the text editor (File Format | UTF8).

The IDE (in Delphi 2009) also seems not to have an option to set the default text format to something other than ANSI. (See How can I set the default file format in the Delphi IDE to UTF8?)


The short answer:

Set the file format for your source code to UTF-8.

(The Delphi 2009 IDE does not use Unicode by default, I guess this is still the same in Delphi 2010)

Community
  • 1
  • 1
mjn
  • 36,362
  • 28
  • 176
  • 378
  • I took the liberty of editing the question for the OP, to at least make the title of the question saner. – Warren P Jun 08 '11 at 07:12
0

If you migrated your project from an old Delphi version, check the font used in your dialogs. Not all fonts support all unicode characters.

I had a problem, which was apparently similar to yours: I had correct unicode strings in the debugger but in the application some of the special characters appeared as black squares. This was with an old program migrated from Delphi 6 where some captions where set through code. On new forms everything was ok (here the captions were set in the dialog editor, but this isn't actually relevant).

The problem was the font of all migrated forms. Delphi 6 used to use "MS Sans Serif" as font. But this font is missing most of the unicode characters. Switching to "Tahoma" solved my problem.

Name
  • 3,430
  • 4
  • 30
  • 34