2

I have an old programm which was programmed in Delphi 1 (or 2, I'm not sure) and I want to build a 64-bit version of it (I use the Delphi XE2). Now the problem is that in the source code there are on the one hand strings and on the other arrays of strings (I guess to limit the string length). Now there are a lot of errors while compiling because of incompatible types. Above all there are procedures which should handle both types. Is there an easy way to solve this problem (without changing every variable)?

3
  • 5
    Show the code and errors you get. Commented Oct 13, 2011 at 8:52
  • I'm guessing these are not arrays of strings, rather short strings declared like this: s: string[10] – but show us some code all the same Commented Oct 13, 2011 at 8:59
  • 2
    ..Is there an easy way to solve this problem (without changing every variable).The major issue related to strings and old delphi applications is the unicode support added in delphi 2009, so you must focus in this point, also it is impossible to give you an easy way to upgrade an old application to Unicode(New versions of Delphi) without knowing the actual code. Now the best way to deal with the conversion to Unicode is learn how Unicode support is implemented in Delphi, an excellent resource is the Delphi and Unicode Whitepaper edn.embarcadero.com/article/38980 Commented Oct 13, 2011 at 9:22

1 Answer 1

3

Short answer
Search and replace : string => : ansistring
make sure you use length(astring) and setLength(astring) instead of manipulating string[0].

Long answer

Delphi 1 has only one type of string.
The old-skool ShortString that has a maximum length of 255 chars and a declared maximum length.

It looks and feels like an array of char, but it has a leading length byte.

var
  ShortString: string[100]; 

In Delphi 2 longstrings (aka AnsiString) were introduced, these replace the shortstring. They do not have a fixed length, but are allocated dynamically instead and automatically grow and shrink as needed.
They are automatically created and destroyed.

var
  Longstring: string; //AnsiString, can have any length up to 2GB.

In Delphi 2009 Unicode was introduced.
This changes the longstring because now each char no langer takes up 1 byte, but takes 2 bytes(*). Additionally you can specify a character set to an AnsiString, whereas the new Unicode longstring uses UTF-16.

What you need to do depends on your needs:

If you just want the old code to work as before and you don't care about supporting all the multilingual stuff Unicode supports, you will need to replace all your string keywords with AnsiString (for all strings that are longstrings).
If you have Delphi 1 code, you can rename the string to ShortString.
I would recommend that you refactor the code to always use longstrings (read: AnsiString) though.

Delphi will automatically translate the UnicodeStrings that all return values of functions (Unicode string) are translated into AnsiStrings and visa versa, however this may include loss of data if your users enter symbols in a editbox that your AnsiString cannot store.
Also all that translation takes a bit of time (I doubt you will notice this though).

In Delphi 1 up to Delphi 2007 this problem did not exist, because controls did not allow Unicode characters to be entered.

(*) gross oversimplification

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.