I have a file opened in TMemoryStream
. Its current encoding can be ANSI or UTF8 with BOM. I have to convert the encoding of TMemoryStream
to UTF8
. How do I do that?
If you are able to change the TMemoryStream
to its descendant TBytesStream
you can just use the Convert
function from TEncoding
.
var
stream: TBytesStream;
bytes: TBytesStream;
...
TEncoding.GetBufferEncoding(stream.Bytes, curEncoding);
if curEncoding <> TEncoding.UTF8 then begin
bytes := TEncoding.Convert(curEncoding, TEncoding.UTF8, stream.Bytes);
stream.Free;
stream := TBytesStream.Create(bytes);
end;
Not sure if it is the most efficient way, but at least it is one way and it only needs a couple of lines, which in turn is also some sort of efficiency.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.