English 中文(简体)
精简
原标题:AnsiString To Stream

I created the following code:

Function AnsiStringToStream(Const AString: AnsiString): TStream;
Begin
  Result := TStringStream.Create(AString, TEncoding.ANSI);
End;

但I m “W1057 强行从AnsiString引向“扼杀”

他有什么错误?

谢谢。

最佳回答

在D2009+,TStringStream 预计UnicodeString/code>,而不是AnsiString。 如果你只想写<条码>AnsiString的内容,就不必将数据转换为Unicode,然后回给Ansi,则使用<条码>TMemoryStream:

function AnsiStringToStream(const AString: AnsiString): TStream; 
begin 
  Result := TMemoryStream.Create;
  Result.Write(PAnsiChar(AString)^, Length(AString));
  Result.Position := 0; 
end; 

由于AnsiString是D2009+的编码网页-aware,被传至您职务的ANY string将被迫服从于本组织违约的Ansi encoding。 如果您希望能通过任何8个轨道型号,例如UTF8String,而不将数据作任何改动,则使用<代码>RawByteString而不是AnsiString:

function AnsiStringToStream(const AString: RawByteString): TStream; 
begin 
  Result := TMemoryStream.Create;
  Result.Write(PAnsiChar(AString)^, Length(AString));
  Result.Position := 0; 
end; 
问题回答

The TStringStream constructor expects a string as its parameter. When you give it an an AnsiString instead, the compiler has to insert conversion code, and the fact that you ve specified the TEncoding.ANSI doesn t change that.

Try it like this instead:

Function AnsiStringToStream(Const AString: AnsiString): TStream;
Begin
  Result := TStringStream.Create(string(AString));
End;

这需要明确的转换,使汇编者了解如何照顾到与编码相关的工作。





相关问题
determining the character set to use

my delphi 2009 app has a basic translation system that uses GNUGetText. i had used some win API calls to prepare the fonts. i thought it was working correctly until recently when someone from Malta ...

Help with strange Delphi 5 IDE problems

Ok, I m going nuts here. For the last (almost) four years, I ve been putting up with some extremely bad behavior from my Delphi 5 IDE. Problems include: Seemingly random errors in coride50.bpl ...

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How convert string to integer in Oxygene

In Delphi, there is a function StrToInt() that converts a string to an integer value; there is also IntToStr(), which does the reverse. These functions doesn t appear to be part of Oxygene, and I can ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签