* * *

Author Topic: UTF8 and numeric to string conversion  (Read 2521 times)

hy

  • Full Member
  • ***
  • Posts: 213
UTF8 and numeric to string conversion
« on: January 14, 2018, 05:30:26 pm »
Hi I am having a program that handles widestrings most of the time correctly (lots of namens with diacritical characters handled correctly). But there are two places in the code where I need to display the value of an Angle in Degrees '°'.
I tried several conversions to no avail. The general Idea is:
Code: Pascal  [Select]
  1. var
  2.       fAngle : double;
  3.       sAngle,
  4.       sCenterLabel   : string;
  5. begin
  6.      fAngle := calculateAngle;
  7.      sAngle := FloatToStr(fAngle) ;
  8.      sCenterLabel := sAngle +'°'
  9. end;
Actually the '°' is always replaced by a questionmark
Any help apreciated
Note:
if I do the following assignment:
Code: Pascal  [Select]
  1.  sCenterLabel := '23,3'+'°';
  2.  
It works so I think the result of floatToStr is a short and the rest is trated as shortstring as well.
« Last Edit: January 14, 2018, 05:35:00 pm by hy »
_____
***hy
OS: debian sid(64bit)  [fpc 3.0.4] Lazarus SVN

wp

  • Hero Member
  • *****
  • Posts: 4830
Re: UTF8 and numeric to string conversion
« Reply #1 on: January 14, 2018, 05:44:49 pm »
... handles widestrings ...
... is trated as shortstring as well...
What are you doing? Why "widestring", why "shortstring"? Simply use string, and everything works fine.

There's really no need to work with widestrings unless you call such functions in a dll or make system calls on Windows (but you are on Linux anyway). And shortstring is technology of the last century.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

hy

  • Full Member
  • ***
  • Posts: 213
Re: UTF8 and numeric to string conversion
« Reply #2 on: January 14, 2018, 05:47:28 pm »
What are you doing? Why "widestring", why "shortstring"? Simply use string, and everything works fine.
I do not have any widestrings or shortstrings in my code.
But the result of floatToStr is obviously not able to accept other than ansi.
_____
***hy
OS: debian sid(64bit)  [fpc 3.0.4] Lazarus SVN

wp

  • Hero Member
  • *****
  • Posts: 4830
Re: UTF8 and numeric to string conversion
« Reply #3 on: January 14, 2018, 06:09:12 pm »
But the result of floatToStr is obviously not able to accept other than ansi.

No, try attached demo. The bug must be somewhere else.

I see in your signature that you use fpc 2.6.4 which still has the old string routines. Upgrade to 3.0.4, maybe this resolves your issue magically.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

hy

  • Full Member
  • ***
  • Posts: 213
Re: UTF8 and numeric to string conversion
« Reply #4 on: January 14, 2018, 06:15:58 pm »
The tag line is not updated i am on fpc 3.0.4

i also get
Code: Pascal  [Select]
  1. ym_main.pas(5682,39) Warning: Implicit string type conversion with potential data loss from "WideString" to "AnsiString"
The message above is the assignment of FloatToStr()...

Code: Pascal  [Select]
  1. ym_main.pas(5681,26) Warning: Implicit string type conversion from "AnsiString" to "WideString"
  2.  
The message above is the assignment of sCenterLabel := sAngle + '°';
_____
***hy
OS: debian sid(64bit)  [fpc 3.0.4] Lazarus SVN

Bart

  • Hero Member
  • *****
  • Posts: 3038
    • Bart en Mariska's Webstek
Re: UTF8 and numeric to string conversion
« Reply #5 on: January 14, 2018, 06:46:10 pm »
Are you using mode delphiunicode or modeswitch unicodestrings?

Bart

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3412
  • I like bugs.
Re: UTF8 and numeric to string conversion
« Reply #6 on: January 14, 2018, 06:46:31 pm »
You have defined mode DelphiUnicode or other similar setting.
Just use the defaults and it should work like magic. See :
 http://wiki.lazarus.freepascal.org/Unicode_Support_in_Lazarus

Remember, if you have a console program (no LCL dependency) you must use LazUTF8 explicitly.

Bart

  • Hero Member
  • *****
  • Posts: 3038
    • Bart en Mariska's Webstek
Re: UTF8 and numeric to string conversion
« Reply #7 on: January 14, 2018, 07:11:26 pm »
@Juha: beat you by 21 seconds  O:-)

Bart

hy

  • Full Member
  • ***
  • Posts: 213
Re: UTF8 and numeric to string conversion
« Reply #8 on: January 14, 2018, 08:07:39 pm »
I have
{$MODE OBJFPC}{$j+}{$H+}
_____
***hy
OS: debian sid(64bit)  [fpc 3.0.4] Lazarus SVN

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3412
  • I like bugs.
Re: UTF8 and numeric to string conversion
« Reply #9 on: January 14, 2018, 08:52:47 pm »
I have
{$MODE OBJFPC}{$j+}{$H+}
I guess you must upload us a full project demonstrating the problem. Only sources, zipped.

hy

  • Full Member
  • ***
  • Posts: 213
Re: UTF8 and numeric to string conversion
« Reply #10 on: January 14, 2018, 09:36:28 pm »
I have
{$MODE OBJFPC}{$j+}{$H+}
I guess you must upload us a full project demonstrating the problem. Only sources, zipped.
106.500 lines of code....? >200 units You wont' be able to analyze
But I will try to reduce it as possible
_____
***hy
OS: debian sid(64bit)  [fpc 3.0.4] Lazarus SVN

jamie

  • Hero Member
  • *****
  • Posts: 786
Re: UTF8 and numeric to string conversion
« Reply #11 on: January 14, 2018, 09:48:02 pm »
Var
 MyDegreeStr :String;

Begin

 MyDegreeStr := 'ALT+167';

hy

  • Full Member
  • ***
  • Posts: 213
Re: UTF8 and numeric to string conversion
« Reply #12 on: January 14, 2018, 10:28:55 pm »
Var
 MyDegreeStr :String;

Begin

 MyDegreeStr := 'ALT+167';
Nice try, aber der string beinhaltet dann auch wirklich  'ALT+167'

_____
***hy
OS: debian sid(64bit)  [fpc 3.0.4] Lazarus SVN

Lutz Mändle

  • New member
  • *
  • Posts: 45
Re: UTF8 and numeric to string conversion
« Reply #13 on: January 14, 2018, 11:23:20 pm »
What is the encoding of the source file? Check it with right clicking in the source editor, then choose Filesettings->Encoding (Dateieinstellungen->Zeichenkodierung) in the popup menu.


hy

  • Full Member
  • ***
  • Posts: 213
Re: UTF8 and numeric to string conversion
« Reply #14 on: January 15, 2018, 12:26:42 am »
UTF8 BOM
_____
***hy
OS: debian sid(64bit)  [fpc 3.0.4] Lazarus SVN

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus