0

In Delphi 7 (for a lot of reasons I can't convert the application in Xe) I must generate the RTF string from a Chinese string that is stored into a UTF8 field (in Firebird 2.5 table).

For example I read the field value that contain the UTF8 value of the string "史蒂芬·克拉申" string into a wiredstring and then I should convert to a string like this 'ca\'b7\'b5\'d9\'b7\'d2\f1\'b7\f0\'bf\'cb\'c0\'ad\'c9\'ea\

The value of UTF8 field for the previous Chinese string is 'å²è’‚芬·克拉申'

How can I do that ?

I have done a lot of search but I haven't find solutions.

Please give me some advice to solve this problem.

Thanks Massimo

Steve Friedl
  • 3,929
  • 1
  • 23
  • 30
HappyMax
  • 1
  • 2
  • https://www.zopatista.com/python/2012/06/06/rtf-and-unicode/ – David Heffernan Feb 06 '20 at 19:53
  • @DavidHeffernan - thank you but anyway I'm not able to translate python code to delphi; – HappyMax Feb 07 '20 at 14:09
  • It's not about the code, but about how to encode Unicode in RTF. And you are doing it wrong. You should be encoding UTF16 values. – David Heffernan Feb 07 '20 at 14:17
  • Sorry but I don't do anything; I just wanted to know how to translate the contents of a UTF8 string field into an RTF string using delphi 7. Thanks – HappyMax Feb 07 '20 at 14:50
  • What you need to do is use the UTF-16 RTF encoding as described in that article. You'll notice that it differs from the RTF encoding that you have in the question. – David Heffernan Feb 07 '20 at 15:05
  • Sorry, I can't convert UTF8 to UTF16; I did many tests without being able to find a solution. Could you indicate a function in Delphi 7 to carry out the conversion? Also after having done it, do you know if there is an application to be able to generate an RTF file with this format ( \uN? control sequence; backslash ‘u’ followed by a signed 16-bit integer value in decimal and a placeholder character (represented here by a question mark)? I doubt that Delphi 7 cannot be used. Thank you. – HappyMax Feb 10 '20 at 11:45
  • `MultiByteToWideChar` does that conversion. You then have to be careful to ensure that you convert each 16 bit `WideChar` to a signed decimal integer. `IntToStr(Smallint(C))` where `C` is a `WideChar`. – David Heffernan Feb 10 '20 at 14:26

1 Answers1

0

After several days I finally found a solution inspired by this post Getting char value in Delphi 7 and the conversion to decimal available on this site https://www.branah.com/unicode-converter

After loading the UTF8 value from the field I use UTF8Decode and then I convert to Rtf ..

var
  fUnicode : widestring;

  function UnicodeToRtf(const S: WideString): string;
   var
     I: Integer;
   begin
     Result := '';
     for I := 1 to Length(S) do
         Result := Result + '\u' + IntToStr(Word(S[I]))+'?';
   end;

begin
   fUnicode := Utf8Decode(DbChineseField.AsString);
   fRtfString := UnicodetoRtf(fUnicode);
   ..... 
end;
HappyMax
  • 1
  • 2