Mega Code Archive

 
Categories / Delphi / Examples
 

Porting strings from 16 to 32-bit

Question: When I port my code from 16 to 32 bit, record types that contain strings do not function correctly. What has happened? Answer: Traditionally, strings that are defined with no length parameter would default to having a length of 255 characters. With the advent of 32 bit Delphi, strings with no defined length default to a long string. Long strings are pointers to a reference counted AnsiString. Long string types are not recommended for use in structures that are intended to be written or read from a disk file. To port your code correctly, you need to explicitly set the expected length of the string in the record, otherwise, a long string is used in place of the original string in the record, leading to unexpected results. Example: ShortStringRec : record s : string[255]; {this uses a short string - ok} end; LongStringRec : record s : string; {this uses a long string - not ok} end;