Delphi announces ansi string size

Easy to define string size 3 (old delphi code)

st:string[3];

we want to move the code to ansi

st:ansiString[3];

will not work!

and for adcanced oem type

st:oemString[3]; 

same problem where

type
  OemString = Type AnsiString(CP_OEMCP);

how can one declare ansi string with fixed length and new type oem?

update: I know that it will create a string with a fixed length. it is part of the design of error protection software and is essential to the program.

+3
source share
5 answers

You do not need to determine the size of AnsiString.

Designation

string[3] 

- for short strings used by Pascal (and Delphi 1), and it is mostly stored for inheritance purposes.

Short strings can be 1 to 255 bytes long. The first ("hidden") byte contains the length.

AnsiString - (0 ). , . , .

UnicodeStrings AnsiStrings, unicode ( 2 ). (Delphi 2009) UnicodeString.

AnsiString ( 127), , CP_OEMCP:

OemString = Type AnsiString(CP_OEMCP);
+5

" " - "Ansi", pre-Delphi.

       st: string[3];

Ansi/ Char Set, Delphi 2009.

, AnsiString. . -.

AnsiString, , , , , string[...].

Short String AnsiString . "", .

a Short String

  st[0] = length(st)
  st[1] = 1st char (if any) in st
  st[2] = 2nd char (if any) in st
  st[3] = 3rd (if any) in st

AnsiString UnicodeString:

  st = nil   if st=''
  st = PAnsiChar if st<>''

PSt: PAnsiChar:

  PWord(PSt-12)^ = code page
  PWord(PSt-10)^ = reference count
  PInteger(PSt-8)^  = reference count
  PInteger(PSt-4)^  = length(st) in AnsiChar or UnicodeChar count
  PAnsiChar(PSt) / PWideChar(PSt) = Ansi or Unicode text stored in st, finished by a #0 char (AnsiChar or UnicodeChar)

, AnsiString UnicodeString, Short String .

+4

, String [3] Unicode Delphi 3 WideChars. , , , :

st: array[1..3] of AnsiChar;
+2

But the old ShortString type, the new line types in Delphi are dynamic. They grow and shrink as needed. You can redirect the string to a given length by calling SetLength (), which is useful to avoid memory reallocation if you need to add piecemeal data to a string that you know is of finite length, but even after that, the string can still grow and shrink when added or deleting data. If you need static strings, you can use an array of [0..n] characters, the size of which will not change dynamically.

+1
source

All Articles