【发布时间】:2012-07-27 15:20:19
【问题描述】:
使用 Delphi XE2 将 TMemoryStream(包含 unicode 字符串)复制到另一个 TMemoryStream 时,我遇到了一个奇怪的行为:
我有两个 TMemoryStream 实例。第一个实例包含 unicode 文本 (SourceMS)。我将一些任意数据写入第二个 MemoryStream (DestMS),然后将第一个流的内容复制到第二个流,如下所示:
var
SomeInt: Integer;
SomeByte: Byte;
SourceMS, DestMS: TMemoryStream;
begin
...
DestMS.Write(SomeInt, SizeOf(SomeInt));
DestMS.Write(SomeByte, SizeOf(SomeByte));
SourceMS.SaveToFile('c:\SourceMS.txt'); // SourceMS.txt contains the unicode chars
DestMS.CopyFrom(SourceMS, 0); // copy the whole content of SourceMS to DestMS
DestMS.SaveToFile('c:\DestMS.txt'); // DestMS.txt DOEST NOT contain unicode chars
end;
如何将第一个流的内容复制到第二个流而不丢失 unicode(具有隐式转换)? 当我说“丢失 unicode”时,我的意思是:unicode 字符串确实被复制到第二个流中,但是 unicode 丢失了。我只得到 ANSI 字符。
【问题讨论】:
-
您的
MS1和MS2变量在您的示例中是否正确排序? -
当您说
..I write some arbitrary data to the second MemoryStream and then copy the contents of the first stream to the second stream时,您是否知道您正在替换第二个流的BOM? -
@James:这只是一段代码。调用 CopyFrom 方法时,MS2 已经填充了数据。
-
@RRUZ:是的,我想到了。但我看不出将 BOM 写入第二个流将如何解决该问题。我以前从未将 BOM 写入任何流,并且从/向流复制从未给我这种行为。
-
@James:我把代码改得更清楚了
标签: delphi delphi-xe2