Search code examples
delphiunicodedelphi-xe

How to convert a string or character to its unicode representation and back?


I'm in the process of learning more about Unicode. Could someone please demonstrate how to translate a code point value to a character string and vice-versa.

For example: How do you convert U+0037 to its character or string representation which is 7.

Please also show how to do this for ascii. For example: convert   to its character or string representation which is a space.


Solution

  • Delphi strings already use Unicode (UTF16) encoding, so there is no need to "convert" delphi strings to Unicode. Here is an example how to insert a Unicode representation of nbsp (U+00A0) and '7' (U+0037') into a delphi string directly:

    procedure TForm1.Button1Click(Sender: TObject);
    const
      U_nbsp = $00A0;
      U_7 = $0037;
    
    var
      S: string;
    
    begin
      S:= 'abcd' + Char(U_nbsp) + Char(U_7);
      ShowMessage(S);
    end;