M
MAGXJ500T
Unregistered / Unconfirmed
GUEST, unregistred user!
下面的代码我将一个字符串写入一个TMemoryStream中,然后再读出来。
var
Stream : TMemoryStream;
Str : String;
iLen : Integer;
b : TBytes;
begin
Stream := TMemoryStream.Create;
try
//写
Str := '你好';
iLen := Length(Str);
Stream.WriteBuffer(iLen , SizeOf(iLen));
Stream.WriteBuffer(Str, iLen);
//读
Stream.Position := 0;
Stream.ReadBuffer(iLen, SizeOf(iLen));
SetLength(Str, iLen);
Stream.ReadBuffer(Str, iLen);
//显示
ShowMessage(Str);
finally
Stream.Free;
end;
end;
但是如果我开始Str = '1',则出错。后来我发现只要字符串长度小于
或等于2都会出现这个问题,请高手指点!
var
Stream : TMemoryStream;
Str : String;
iLen : Integer;
b : TBytes;
begin
Stream := TMemoryStream.Create;
try
//写
Str := '你好';
iLen := Length(Str);
Stream.WriteBuffer(iLen , SizeOf(iLen));
Stream.WriteBuffer(Str, iLen);
//读
Stream.Position := 0;
Stream.ReadBuffer(iLen, SizeOf(iLen));
SetLength(Str, iLen);
Stream.ReadBuffer(Str, iLen);
//显示
ShowMessage(Str);
finally
Stream.Free;
end;
end;
但是如果我开始Str = '1',则出错。后来我发现只要字符串长度小于
或等于2都会出现这个问题,请高手指点!