AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Sprachen und Entwicklungsumgebungen Object-Pascal / Delphi-Language Delphi D2009: Altes Projekt - String zu ANSI oder UTF8String?
Thema durchsuchen
Ansicht
Themen-Optionen

D2009: Altes Projekt - String zu ANSI oder UTF8String?

Ein Thema von Zacherl · begonnen am 31. Aug 2008 · letzter Beitrag vom 31. Aug 2008
Antwort Antwort
Benutzerbild von Zacherl
Zacherl

Registriert seit: 3. Sep 2004
4.629 Beiträge
 
Delphi 10.2 Tokyo Starter
 
#1

D2009: Altes Projekt - String zu ANSI oder UTF8String?

  Alt 31. Aug 2008, 13:26
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
  Mit Zitat antworten Zitat
mkinzler
(Moderator)

Registriert seit: 9. Dez 2005
Ort: Heilbronn
39.851 Beiträge
 
Delphi 11 Alexandria
 
#2

Re: D2009: Altes Projekt - String zu ANSI oder UTF8String?

  Alt 31. Aug 2008, 13:28
Das 2. ist ein Unicodetyp das erstere nicht.
Markus Kinzler
  Mit Zitat antworten Zitat
Benutzerbild von Zacherl
Zacherl

Registriert seit: 3. Sep 2004
4.629 Beiträge
 
Delphi 10.2 Tokyo Starter
 
#3

Re: D2009: Altes Projekt - String zu ANSI oder UTF8String?

  Alt 31. Aug 2008, 13:32
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.
  Mit Zitat antworten Zitat
Apollonius

Registriert seit: 16. Apr 2007
2.325 Beiträge
 
Turbo Delphi für Win32
 
#4

Re: D2009: Altes Projekt - String zu ANSI oder UTF8String?

  Alt 31. Aug 2008, 13:35
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.
Wer erweist der Welt einen Dienst und findet ein gutes Synonym für "Pointer"?
"An interface pointer is a pointer to a pointer. This pointer points to an array of pointers, each of which points to an interface function."
  Mit Zitat antworten Zitat
Benutzerbild von Zacherl
Zacherl

Registriert seit: 3. Sep 2004
4.629 Beiträge
 
Delphi 10.2 Tokyo Starter
 
#5

Re: D2009: Altes Projekt - String zu ANSI oder UTF8String?

  Alt 31. Aug 2008, 13:40
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?
  Mit Zitat antworten Zitat
Benutzerbild von toms
toms
(CodeLib-Manager)

Registriert seit: 10. Jun 2002
4.648 Beiträge
 
Delphi XE Professional
 
#6

Re: D2009: Altes Projekt - String zu ANSI oder UTF8String?

  Alt 31. Aug 2008, 13:40
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/)
Thomas
  Mit Zitat antworten Zitat
Benutzerbild von Zacherl
Zacherl

Registriert seit: 3. Sep 2004
4.629 Beiträge
 
Delphi 10.2 Tokyo Starter
 
#7

Re: D2009: Altes Projekt - String zu ANSI oder UTF8String?

  Alt 31. Aug 2008, 13:45
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.
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 07:04 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