Recent

Author Topic: Conversion to UnicodeString  (Read 884 times)

jcmontherock

  • Sr. Member
  • ****
  • Posts: 272
Conversion to UnicodeString
« on: July 20, 2024, 05:15:04 pm »
I have the following instructions:
Code: Pascal  [Select][+][-]
  1. aStr  := '  -- --------------------------------------------------------      ' + sLineBreak;
  2. aStr += '  -- Hôte:                         127.0.0.1                       ' + sLineBreak;

aStr is defined as an AnsiString.

For only the 2nd line I got the following warning messages:
Warning: Implicit string type conversion from "AnsiString" to "UnicodeString"
Warning: Implicit string type conversion with potential data loss from "UnicodeString" to "AnsiString"

I do not understand why the compiler want to convert the second line to UnicodeString and then to AnsiString. Caused by the concatenation ?
« Last Edit: July 20, 2024, 05:16:36 pm by jcmontherock »
Windows 11 UTF8-64 - Lazarus 4RC1-64 - FPC 3.2.2

Seenkao

  • Hero Member
  • *****
  • Posts: 613
    • New ZenGL.
Re: Conversion to UnicodeString
« Reply #1 on: July 20, 2024, 05:26:53 pm »
Code: Pascal  [Select][+][-]
  1. aStr = aStr + '  -- Hôte:                         127.0.0.1                       ' + sLineBreak;
same errors?
Rus: Стремлюсь к созданию минимальных и достаточно быстрых приложений.

Eng: I strive to create applications that are minimal and reasonably fast.
Working on ZenGL

tetrastes

  • Hero Member
  • *****
  • Posts: 595
Re: Conversion to UnicodeString
« Reply #2 on: July 20, 2024, 06:50:56 pm »
I suppose you use {$codepage utf8} directive or -FcUTF8 option. In this case compiler converts string constants with non-ASCII characters to UnicodeString (UTF-16). See produced asm:
Code: ASM  [Select][+][-]
  1. # Begin asmlist al_typedconsts
  2.  
  3. .section .rodata.n_.Ld1,"d"
  4.         .balign 8
  5. .Ld1$strlab:
  6.         .short  0,1
  7.         .long   0
  8.         .quad   -1,69
  9. .Ld1:
  10.         .ascii  "  -- ----------------------------------------------"
  11.         .ascii  "----------      \015\012\000"
  12.  
  13. .section .rodata.n_.Ld2,"d"
  14.         .balign 8
  15. .Ld2$strlab:
  16.         .short  1200,2
  17.         .long   0
  18.         .quad   -1,69
  19. .Ld2:
  20.         .short  32,32,45,45,32,72,244,116,101,58,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32
  21.         .short  32,49,50,55,46,48,46,48,46,49,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,13,10,0
  22. # End asmlist al_typedconsts
  23.  

Tell compiler that this string constant is AnsiString explicitly:
Code: Pascal  [Select][+][-]
  1.   aStr += AnsiString('  -- Hôte:                         127.0.0.1                       ') + sLineBreak;
  2.  
Asm:
Code: ASM  [Select][+][-]
  1. # Begin asmlist al_typedconsts
  2.  
  3. .section .rodata.n_.Ld1,"d"
  4.         .balign 8
  5. .Ld1$strlab:
  6.         .short  0,1
  7.         .long   0
  8.         .quad   -1,69
  9. .Ld1:
  10.         .ascii  "  -- ----------------------------------------------"
  11.         .ascii  "----------      \015\012\000"
  12.  
  13. .section .rodata.n_.Ld2,"d"
  14.         .balign 8
  15. .Ld2$strlab:
  16.         .short  65001,1
  17.         .long   0
  18.         .quad   -1,70
  19. .Ld2:
  20.         .ascii  "  -- H\303\264te:                         127.0.0.1"
  21.         .ascii  "                       \015\012\000"
  22. # End asmlist al_typedconsts
  23.  

jcmontherock

  • Sr. Member
  • ****
  • Posts: 272
Re: Conversion to UnicodeString
« Reply #3 on: July 21, 2024, 09:54:15 am »
@seenkao: yes same messages.

@tetrastes: Yes, I use {$codepage utf8}. If I comment this directive all the warning messages disappear and the cmd program works, as well, fine. I will find the utility of that command in programs, where text is in utf-8 encoding in both cases.

Thanks a lot both of you.
Windows 11 UTF8-64 - Lazarus 4RC1-64 - FPC 3.2.2

 

TinyPortal © 2005-2018