Home Convert encoding of TMemoryStream to utf8
Reply: 1

Convert encoding of TMemoryStream to utf8

user3857505 Published in 2017-11-10 11:37:03Z

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?

Uwe Raabe
Uwe Raabe Reply to 2017-11-13 10:04:02Z

If you are able to change the TMemoryStream to its descendant TBytesStream you can just use the Convert function from TEncoding.

  stream: TBytesStream;
  bytes: TBytesStream;
  TEncoding.GetBufferEncoding(stream.Bytes, curEncoding);
  if curEncoding <> TEncoding.UTF8 then begin
    bytes := TEncoding.Convert(curEncoding, TEncoding.UTF8, stream.Bytes);
    stream := TBytesStream.Create(bytes);

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.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.391312 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO