Recent

Author Topic: Converting UCS2 to UTF8 via Streams  (Read 1393 times)

majlumbo

  • New Member
  • *
  • Posts: 14
Converting UCS2 to UTF8 via Streams
« on: January 06, 2020, 10:36:53 pm »
I have a MemoryStream with a portion of a UCS2 File (I've extracted one line from a FileStream to the MemoryStream).  I am using the funtion UTF16ToUTF8 contained in LazUTF8.pas to convert the string to UTF8 with the following code
Code: [Select]
var
  Line: String;
  FS: TFileStream;
  PosOfSeparator: Int64;
begin
  ...
  MS := TMemoryStream.Create;
  try
    MS.CopyFrom(FS {created earlier}, PosOfSeparator {# bytes to read - initialized earlier});
    Line := UTF16ToUTF8(PWideChar(MS.Memory), MS.Size dive SizeOf(WideChar));
  finally
    MS.Free;
  end;
  ..
end;

This works reasonably well until I have characters that are meant to be read from RIGHT TO LEFT, e.g. arabic.  When I encounter arabic characters, I correctly get the arabic characters, but all the non-arabic characters that are AFTER the arabic characters, appear BEFORE the arabic characters in my LINE variable above.

Is there another method I can convert the UCS2 String to UTF8?
« Last Edit: January 07, 2020, 04:20:33 am by majlumbo »

 

TinyPortal © 2005-2018