delphi Berlin10.1 string、AnsiString及Tbytes等之间的转换

1.类型说明

Byte:Represents an 8-bit unsigned integer type.

[PByte:Is a pointer to a Byte.]

Char:Represents a word-sized (16-bits) character type

AnsiChar:Represents a byte-sized (8-bits) character type

WideChar:Represents a word-sized (16-bits) character type.

[PChar:Defines a null-terminated string]

[PAnsiChar:Defines a null-terminated ANSI string.]

String:Represents an alias for the generic UnicodeString type.

AnsiString:Represents a dynamically allocated string whose maximum length is limited only by available memory.

WideString:A null-terminated string of wide characters, with no reference counting.

[PString:Is a pointer to a String.]

[PAnsiString:Is a pointer to an AnsiString.]

TBytes:declares an array of Bytes.

2.指针

1 var

2 X, Y: Integer; // X and Y are Integer variables

3 P: ^Integer; // P points to an Integer

4 begin

5 X := 17; // assign a value to X

6 P := @X; // assign the address of X to P

7 Y := P^; // dereference P; assign the result to Y

8 end;

type

PInteger = ^Integer;

var

R: Single;

I: Integer;

P: Pointer;

PI: PInteger;

begin

...

P := @R;

PI := PInteger(P);

I := PI^;

end;

3.一些点

SizeOf:Returns the number of bytes occupied by a variable or type.

Length:Returns the number of characters in a string or of elements in an array.

Move:Copies bytes from a source to a destination. e.g.: Move(PChar(@arrBuf[0])^, LPChar^, aSectorNum * 512);

ByteLength:Returns the length of a given string in bytes.

GetBytes:Encodes a set of characters into a sequence of bytes

FillChar:Fills contiguous bytes with a specified value.

4.转换

TBytes -> PChar : LPChar := PChar(LTBytes);

PChar -> TBytes:LTBytes := BytesOf(LPChar);

TBytes -> Array of AnsiChar:move(LTBytes[i],LArrayOfAnsiChar[k]);

Array of AnsiChar -> string:LString := StringOf(BytesOf(LArrayOfAnsiChar));