|
一> 将一个长整型,转换成字节:
- function TForm1.longToBytes(value: Longint): TBytes;
- begin
- SetLength(result, sizeOf(value));
- move(value, result[0], sizeOf(value));
- end;
复制代码 因为长整型有4个字节,所以转出来的字串也有相同的长度。
二> 将字符串转换成字节:
- function TForm1.stringToBytes(const str : string): TBytes;
- var i: integer;
- begin
- setLength(result, length(str));
- for i := 0 to length(str)-1 do
- result[i] := Ord(str[i+1]); // Note: Ord(str[i +1 ] ! 将一个char转换成ascii码
- end;
复制代码
三> 将字节转换成字符串:
- function TForm1.bytesToString(const bytes : TBytes): string;
- var
- i: Integer;
- begin
- setLength(result, length(bytes));
- for i := 0 to length(bytes) - 1 do
- result[i+1] := char(bytes[i]);
- end;
复制代码 以上在Delphi2007中测试通过。
|
|