Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi D2009: Altes Projekt - String zu ANSI oder UTF8String? (https://www.delphipraxis.net/119773-d2009-altes-projekt-string-zu-ansi-oder-utf8string.html)

Zacherl 31. Aug 2008 13:26


D2009: Altes Projekt - String zu ANSI oder UTF8String?
 
Hey,

wenn ich ein altes Projekt habe, welches ich schnell auf D2009 lauffähig machen möchte, dann kann ich ja praktisch einfach alle Variablen des String Typs nach AnsiString konvertieren, und hinter alle APIs ein A setzen.

So meine eigentlich Frage ist jetzt, ob ich AnsiString oder UTF8String verwenden muss und wo genau der Unterschied zwischen den beiden Typen liegt.

Gruß Zacherl

mkinzler 31. Aug 2008 13:28

Re: D2009: Altes Projekt - String zu ANSI oder UTF8String?
 
Das 2. ist ein Unicodetyp das erstere nicht.

Zacherl 31. Aug 2008 13:32

Re: D2009: Altes Projekt - String zu ANSI oder UTF8String?
 
Aber trotzdem ein Byte lang?

Aus der D2007 System.pas:
Delphi-Quellcode:
type
  UTF8String = type string;
Und bei D2007 hat ein Char im String ja Standardmäßig ein Byte.

Apollonius 31. Aug 2008 13:35

Re: D2009: Altes Projekt - String zu ANSI oder UTF8String?
 
Der Unterschied zwischen Ansistring und UTF8String besteht in D2009 meines Wissens in der Interpretation von Char(128) bis Char(255). Die Chars in diesem Bereich sind nämlich für unterschiedliche Codepages verschieden, und UTF8String ist als Ansistring mit einer besonderen Codepage, eben der UTF-8 Codepage, implementiert.

Zacherl 31. Aug 2008 13:40

Re: D2009: Altes Projekt - String zu ANSI oder UTF8String?
 
Ich habe in meiner D2007 Anwendung per Socket ein Bytearray erhalten, welches einen UTF8 String enthielt. Dann habe ich das Array mit CopyMemory in einen String kopiert und die UTF8 Formatierung wurde automatisch übernommen.

Wenn ich das richtig verstehe, kann ich nichts falsch machen, wenn ich den String als UTF8String deklariere. Sollte halt nur ein AnsiString ankommen würde das keinen Unterschied machen oder?

toms 31. Aug 2008 13:40

Re: D2009: Altes Projekt - String zu ANSI oder UTF8String?
 
Interessant in diesem Zusammenhang ist auch dieser Artikel:

Zitat:

For some time, Delphi has had a little-know type called UTF8String. It was little-know, because it didn’t really work as advertised. Try this in Delphi 2007:

var S: UTF8String;
S := "Tiburón";
WriteLn(Length(S))

Though S is declared as UTF8String, it stores the string using the default Windows code page, instead of UTF-8, with a length of 7 bytes. That’s because in Delphi 2007, you’ll find this declaration in System.pas:

type UTF8String = type string;

This means that in Delphi 2007, there’s really no difference between UTF8String and AnsiString. In Delphi 2009, however, you’ll find this declaration:

type UTF8String = type AnsiString(65001);

65001 is the code page number for UTF-8 on the Windows platform. You can declare your own string types this way using any code page understood by the WideCharToMultiByte() and MultiByteToWideChar() API calls. E.g. if you assign a UnicodeString to a UTF8String, WideCharToMultiByte(65001) is called to convert the string from UTF-16 to UTF-8. This is no different than Delphi 2007 (or 2009) calling WideCharToMultiByte(0) when you assign a WideString to an AnsiString.

In Delphi 2009, the code snippet at the top of this post will convert “Tiburón” to UTF-8 at compile time. At runtime, 8 bytes are loaded directly into S. There will be no call to WideCharToMultiByte() at runtime for this literal assignment. The accented ó takes up two bytes when encoded as UTF-8. Length(S) will return 8.

You can easily declare your own typed AnsiStrings in Delphi 2009. If UTF8String is too modern for you, try this:

type EBCDICString = type AnsiString(37);
(Quelle: http://www.micro-isv.asia/2008/08/wi...ring-stand-up/)

Zacherl 31. Aug 2008 13:45

Re: D2009: Altes Projekt - String zu ANSI oder UTF8String?
 
Das hieße ja ich müsste meinen Zielstring in D2009 als UTF8String deklarieren. Klingt gut .. so langsam verstehe ich die ganze Sache.

Was mich eben verwirrt hat war, um deinen Artikel als Grundlage zu nehmen, dass nur bestimmte Zeichen in der UTF8 Codepage zwei Bytes benötigen, wie z.b. Ä oder sowas. Hierbei hat Length(S) allerdings trotzdem 1 zurückgegeben. Da leuchtet es ein, dass bei der allgemeinen Deklaration in D2007 keine Unterschiede feststellbar sind.


Alle Zeitangaben in WEZ +1. Es ist jetzt 01:40 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz