Программирование - это просто
Advertisement
arrow arrow Delphi arrow Delphi (Pchar, WideString, TStream, )
06.02.2023 .
1:
C#
Delphi
Web-
Excel-
.
Delphi (Pchar, WideString, TStream, ) E-mail
megabax   
17.02.2010 .
New Page 1

Delphi (Pchar, WideString, TStream, )

  .

, String Delphi. ShortString, AnsiString. Delphi {$H+} {$LongStrings On}. String AnsiString. {$H-} {$LongStrings Off} String  ShortString.

: "Project" -> "Options"*

  Delphi (Pchar, WideString, TStream, )

"Compiler", "Huge strings"*:

  Delphi (Pchar, WideString, TStream, )

ShortString AnsiString

ShortString -  

sstr: ShortString; - , () . 

  • = 0..255. 
  • : sstr: String[. ];. 
  • : sstr [0], Char, Length(sstr)( ). Ord(sstr[0]) = Length(sstr) 

  • ( @sstr Addr(sstr)) . 

  • (sstr [ i]) 1. 

  • , : Byte(sstr[0]) = SizeOf(sstr). , , , ShortString, 256 . 

  • (.. ). 

     

ShortString PChar #0 : 
 

function ShortStringToPChar (sstr: ShortString): PChar; 
begin  
      sstr := sstr + #0;  
      Result := @sstr[1]; 
end

AnsiString -  
astr: AnsiString; - AnsiChar (, Char, ). , :

var astr: String; - astr AnsiString. 

AnsiString : 

type // , 
    TAnsiString = record // !!!  
    RefCount: LongWord; //  
    Length: LongWord; //  
    Data: array[1..Length+1] of AnsiChar; // ,  
end


AnsiString - , , Data. Szieof, , 4 - .  Delphi . , :

procedure TEasyGameContainer.Serialize(AStream:TStream);
var i,cn:integer; lClassName:string;
begin
   inherited Serialize(AStream);
   cn:=FObjects.Count-1;
   AStream.Write(cn,sizeof(cn));
   for i:=0 to cn do
   begin
      lClassName:=TEasyGameObject(FObjects[i]).ClassName;
     AStream.Write(lClassName,sizeof(lClassName));
     TEasyGameObject(FObjects[i]).Serialize(AStream);
   end;
end;

procedure TEasyGameContainer.Unserialize(AStream:TStream);
var i,cn:integer; lClassName:string;
      lClass:TPersistentClass;
      lObject:TPersistent;
begin
    inherited Unserialize(AStream);
   AStream.Read(cn,sizeof(cn));
   for i:=0 to cn do
   begin
      AStream.Read(lClassName,sizeof(lClassName));
      lClass:=FindClass(lClassName);
      lObject:=lClass.Create;
      TEasyGameObject(lObject).Unserialize(AStream);
      AddGameObject(TEasyGameObject(lObject));
    end;
end;

"". .

, AnsiString, . AnsiString:

PStrRec = ^StrRec;
     StrRec = packed record // 8
     refCnt: Longint; // 4 -
     length: Longint; // 4 -
end;

(TEdit), (TButton) (TLabel), Standart*:

TEdit,   Delphi

OnClick :

procedure TStringsSampleForm.btnAnsiStrDemoClick(Sender: TObject);
var
S: String;
P: PStrRec;
pp: ^char;
begin
    s:=edString.Text;
    P:=Pointer(Integer(S)-8);
    Label1.Caption:='refCnt = ' + IntToStr(P.refCnt);
    Label2.Caption:='length = ' + IntToStr(P.length);
    pp := Pointer(S);
    inc(pp,2);
    Label3.Caption:=' '+pp^;
end;

, :

Pchar, WideString,    Delphi

, . AnsiString ShortString, - : astr[ i]. 

Delphi : , ( {$R+}). . ( ) .  ({$R-} {$RangeChecks Off}), , .. , BufferOverflow- (.. ). {$O+} {$OverflowCheks On}. . 

(Length) SetLength. = 2 (.. - 4 ). 4 ( ). #0 ( ). PChar: PChar(astr). , ! 

AnsiString . PChar AnsiString , ! , ( SysUtils.AnsiPos). ! 

RefCount . . ( somestr := astr, , (AnsiString ) 1 . (somestr := astr + 'A', . 

, , : procedure UniqueString (var str: string). , str , , . PChar API-. , PChar . 

, , . . : ( ) Pointer(astr) := nil; ( , ) : System._LStrClr(S); 

PChar -  

  • pstr: PChar; - (zero-terminated, ). , , #0. ( , char*). 

  • PChar type PChar = ^Char; 

  • ANSI- API- ( CreateFileA). VCL ANSI- API- Windows, CreateFile CreateFileA. SysUtils - API-, String PChar (- PChar ). 

  • Delphi PChar, PAnsiChar PWideChar. 

  • PChar, array of Char. . ! , , #0. 

  • AnsiString PChar , Delphi , (.. 0, ), PChar . PChar (pstr := PChar(astr) , API-. WideString PWideChar. UniqueString . 

  • PChar , AnsiString, - Delphi PChar, , . 

  • PChar AnsiString: astr := patr; . 

 


procedure SetString(var Str: string; Buffer: PChar; Length: Integer); Str Length Length Buffer Str ( Str - , Length 256). Delphi. 

PWideChar 

  • PWideChar - , "" . 

  • 2 . Unicode. - #0. 

  • array of WideChar. . PChar - ! 

  • Unicode- API- ( CreateFileW), OLE COM. 

  • PWideChar String StringToOleStr. , API- SysFreeString. 

 

WideString -  

  • wstr: WideString; - . Unicode, 2 (16- WideChar). 

  • 256 Unicode (WideChar) AnsiChar (Char) . 

  • , AnsiString, WideString , #0 ( PWideChar), , . 

  • Delphi "" "". 

  • WideString AnsiString ANSI. -ANSI- ( 255) , Windows ( ). . API- WideCharToMultiByte. 
     

-  

  • - , 1 ( Windows 2 ). Unicode - , . Unicode 2 , 1 2 ( !). 

  • (, ), 256 . 

  • : , ( ) (.. ). , .. , 'A' . 

  • Delphi , SysUtils, . ( ) SysUtils: ByteType StrByteType: 
     

type TMbcsByteType = ( 
mbSingleByte,//  
mbLeadByte, //  
mbTrailByte);//  

function ByteType (const S: String; Index: Integer): TMbcsByteType; 
  • WideString (Unicode) AnsiString (String): wstr := astr; 

  • ( ) ( ), ( , ). : ? : var SysLocale: TSysLocale; Windows , , SysLocale.FarEast = True. 

  • , . 
     

, resourcestring 
 

  • , ? mystringresource.rc, , res c brcc32.exe,   {$R mystringresource.res} API-. ? Delphi, , . 

  • - : 

     

    resourcestring  
    MyResString = 'Hello World'; 


 

  • Ѩ! , . , . ResHacker' Restorator' . () .

  •   LoadResSring . 

  • , . resourcestring : , - .. .

  • Delphi , resourcestring . 

  • ( SysUtils.Format),   ( , .. ): 
     

resourcestring  
ResErrorMsg = ': %0:s %1:s , - %2:s'; 


resourcestring - PResStringRec, : 
 

type  
    PResStringRec = ^TResStringRec;  
    TResStringRec = packed record  
          Module: ^Cardinal; // ( )  
          Identifier: Integer; //  
    end



 

var 
ResID: Integer; 
ResID := PResStringRec(@MyResString).Indentifier; 

 


, * ,   "Delphi", "Borland Software Corporation".


 

 







 

( 18.02.2013 . )
 
« .   . »
 
© 2023 Программирование - это просто
Joomla! - , GNU/GPL.
© 2005-2008 Joom.Ru - Joomla!
Design by Mamboteam.com | Powered by Mambobanner.de
  .