本文介绍了在Delphi 2009中将TMemoryStream转换为'String'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在Delphi 2009之前有以下代码:

We had the following code prior to Delphi 2009:

<span style="font: 10pt Courier New;"><span class="pas1-reservedword">function</span><span class="pas1-space"> </span><span class="pas1-identifier">MemoryStreamToString(M:</span><span class="pas1-space"> </span><span class="pas1-identifier">TMemoryStream):</span><span class="pas1-space"> </span><span class="pas1-reservedword">String</span><span class="pas1-symbol">;
</span><span class="pas1-reservedword">var
</span><span class="pas1-space">  </span><span class="pas1-identifier">NewCapacity:</span><span class="pas1-space"> </span><span class="pas1-identifier">Longint;
</span><span class="pas1-reservedword">begin
</span><span class="pas1-space">  </span><span class="pas1-reservedword">if</span><span class="pas1-space"> </span><span class="pas1-symbol">(M.Size</span><span class="pas1-space"> </span><span class="pas1-symbol">=</span><span class="pas1-space"> </span><span class="pas1-number">0)</span><span class="pas1-space"> </span><span class="pas1-reservedword">or</span><span class="pas1-space"> </span><span class="pas1-symbol">(M.Memory</span><span class="pas1-space"> </span><span class="pas1-symbol">=</span><span class="pas1-space"> </span><span class="pas1-reservedword">nil</span><span class="pas1-symbol">)</span><span class="pas1-space"> </span><span class="pas1-reservedword">then
</span><span class="pas1-space">    </span><span class="pas1-identifier">Result:=</span><span class="pas1-space"> </span><span class="pas1-string">''
</span><span class="pas1-space">  </span><span class="pas1-reservedword">else
</span><span class="pas1-space">  </span><span class="pas1-reservedword">begin
</span><span class="pas1-space">    </span><span class="pas1-reservedword">if</span><span class="pas1-space"> </span><span class="pas1-identifier">TMemoryStreamProtected(M).Capacity</span><span class="pas1-space"> </span><span class="pas1-symbol">=</span><span class="pas1-space"> </span><span class="pas1-identifier">M.Size</span><span class="pas1-space"> </span><span class="pas1-reservedword">then
</span><span class="pas1-space">    </span><span class="pas1-reservedword">begin
</span><span class="pas1-space">      </span><span class="pas1-identifier">NewCapacity:=</span><span class="pas1-space"> </span><span class="pas1-identifier">M.Size+1;
</span><span class="pas1-space">      </span><span class="pas1-identifier">TMemoryStreamProtected(M).Realloc(NewCapacity);
</span><span class="pas1-space">    </span><span class="pas1-reservedword">end</span><span class="pas1-symbol">;
</span><span class="pas1-space">    </span><span class="pas1-identifier">NullString(M.Memory^)[M.Size]:=</span><span class="pas1-space"> </span><span class="pas1-character">#0;
</span><span class="pas1-space">    </span><span class="pas1-identifier">Result:=</span><span class="pas1-space"> </span><span class="pas1-identifier">StrPas(M.Memory);
</span><span class="pas1-space">  </span><span class="pas1-reservedword">end</span><span class="pas1-symbol">;
</span><span class="pas1-reservedword">end</span><span class="pas1-symbol">;
</span></span>

如何使用Delphi 2009将代码转换为支持Unicode?

How might we convert this code to support Unicode now with Delphi 2009?

推荐答案

即使对于较旧的Delphi版本,您的代码也不必要。为什么要获取流的字符串版本会强制重新分配流的内存?

The code you have is unnecessarily complex, even for older Delphi versions. Why should fetching the string version of a stream force the stream's memory to be reallocated, after all?

function MemoryStreamToString(M: TMemoryStream): string;
begin
  SetString(Result, PChar(M.Memory), M.Size div SizeOf(Char));
end;

该工具适用于所有Delphi版本,而不仅仅是Delphi 2009。特例。 是一个不满意的功能。

That works in all Delphi versions, not just Delphi 2009. It works when the stream is empty without any special case. SetString is an under-appreciated function.

如果流切换到Delphi 2009时流的内容不会改变为Unicode,那么您应该使用此功能:函数MemoryStreamToString(M:TMemoryStream):AnsiString; p>

If the contents of your stream aren't changing to Unicode with your switch to Delphi 2009, then you should use this function instead:

function MemoryStreamToString(M: TMemoryStream): AnsiString;
begin
  SetString(Result, PAnsiChar(M.Memory), M.Size);
end;

相当于您的原始代码,但跳过特殊情况。

That's equivalent to your original code, but skips the special cases.

这篇关于在Delphi 2009中将TMemoryStream转换为'String'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 04:44