Delphi

[Delphi] 배열을 문자열로 변환

그저심 2023. 3. 3. 15:20
  • 배열을 문자열로 변환
    • procedure SetString(var S: String; Buffer: PChar; Length: Integer);
      • Sets the contents and length of the given string. 
        In Delphi code, SetString sets the contents and length of the given string variable to the block of characters given by the Buffer and Length parameters. 

        For a short string variable, SetString sets the length indicator character (the character at S[0]) to the value given by Length and then, if the Buffer parameter is not nil, copies Length characters from Buffer into the string, starting at S[1]. For a short string variable, the Length parameter must be a value from 0 through 255. 

        For a long string variable, SetString sets S to reference a newly allocated string of the given length. If the Buffer parameter is not nil, SetString then copies Length characters from Buffer into the string; otherwise, the content of the new string is left uninitialized. If there is not enough memory available to create the string, an EOutOfMemory exception is raised. Following a call to SetString, S is guaranteed to reference a unique string (a string with a reference count of one). 
  • 문자열을 배열로 변환
    • function StrPCopy(Dest: PAnsiChar; const Source: AnsiString): PAnsiChar;
    • function StrPCopy(Dest: PWideChar; const Source: UnicodeString): PWideChar;
      • Copies an AnsiString (long string) to a null-terminated string. 
        StrPCopy copies Source into a null-terminated string Dest. It returns a pointer to Dest. 
        StrPCopy does not perform any length check. 
        The destination buffer must have room for at least Length(Source)+1 characters.
  • Sample
    • var
        arr: array [0..5] of Byte;
        strAnsi, str2: AnsiString;
      begin
        arr[0] := $31;
        arr[1] := $32;
        arr[2] := $33;
        arr[3] := $34;
        arr[4] := $35;

        SetString(strAnsi, PAnsiChar(@arr[0]), 5);
        ShowMessage(string(strAnsi));

        FillChar(arr, SizeOf(arr), 0);
        StrPCopy(@arr[0], strAnsi);
        str2 := AnsiChar(arr[0]) + AnsiChar(arr[1]) + AnsiChar(arr[2]) + AnsiChar(arr[3]) + AnsiChar(arr[4]);
        ShowMessage(string(str2));

 

반응형