Search code examples
delphiencodingdelphi-6

Delphi 6 convert ucs-2 little endian file to utf8


i am trying to import a csv file that has ucs2 little endian encoding. I am using a jedi compnonent(jvCsvDataset) for collecting-manipulating the data. The component cannot read the data with this encoding and i need to convert it to utf8. The thing is that i need to do it within delphi 6.

Are you aware of a "ucs2decode" function or any other method?

Thanks in advance!


Solution

  • Read the file in a WideString, then use UTF8Encode, perhaps like this:

    function ReadUCS2FileToUTF8(const FilePath: string): UTF8String;
    var
      f: TFileStream;
      d: WideString;
    begin
      f := TFileStream.Create(FilePath, fmOpenRead or fmShareDenyWrite);
      try
        SetLength(d, f.Size div SizeOf(WideChar));
        if Length(d) > 0 then
          f.ReadBuffer(d[1], Length(d) * SizeOf(WideChar));
      finally
        f.Free;
      end;
      Result := UTF8Encode(d);
    end;