1、string转为ansistring 一、直接赋值 (有警告) 二、ansistring()类型强制转换。(无警告) 2、ansistring 转为string 一、直接赋值 (有警告) 二、string()类型强制转换。(无警告) 3、string 转为Tbytes 一、bytes:= bytesof(str) 已转为ansi编码 二、bytes:= widebytesof(str) UNICODE 编码 4、ansistring 转为Tbytes 一、bytes:= bytesof(str) ansi编码 二、bytes:= widebytesof(string(str)) UNICODE 编码 5、Tbytes 转为string 一、 str:=stringof(bytes) Tbytes 为ansi编码 二、 str:=widestringof(bytes) Tbytes 为unicode编码 6、PChar转String 用StrPas函数,StrPas(PChar):AnsiString; {转换 TBytes 到 Integer} procedure TForm1.Button1Click(Sender: TObject); var bs: TBytes; {TBytes 就是 Byte 的动态数组} i: Integer; begin {它应该和 Integer 同样大小才适合转换} SetLength(bs, 4); bs[0] := $10; bs[1] := $27; bs[2] := 0; bs[3] := 0; {由于 TBytes 是动态数组, 因此它的变量 bs 是个指针; 因此先转换到 PInteger} i := PInteger(bs)^; ShowMessage(IntToStr(i)); {10000} end; {从 Bytes 静态数组到 Integer 的转换会方便些} procedure TForm1.Button2Click(Sender: TObject); var bs: array[0..3] of Byte; i: Integer; begin bs[0] := $10; bs[1] := $27; bs[2] := 0; bs[3] := 0; i := Integer(bs); ShowMessage(IntToStr(i)); {10000} end; {转换到自定义的结构} procedure TForm1.Button3Click(Sender: TObject); type TData = packed record a: Integer; b: Word; end; var bs: array[0..5] of Byte; {这个数组应该和结构大小一直} data: TData; begin FillChar(bs, Length(bs), 0); bs[0] := $10; bs[1] := $27; data := TData(bs); ShowMessage(IntToStr(data.a)); {10000} end; {转换给自定义结构的一个成员} procedure TForm1.Button4Click(Sender: TObject); type TData = packed record a: Integer; b: Word; end; var bs: array[0..3] of Byte; data: TData; begin FillChar(bs, Length(bs), 0); bs[0] := $10; bs[1] := $27; data.a := Integer(bs); ShowMessage(IntToStr(data.a)); {10000} end --------------------- 做者:古今飞扬 来源:CSDN 原文:https://blog.csdn.net/GzyCSDN/article/details/77481346 版权声明:本文为博主原创文章,转载请附上博文连接!