Recent

Author Topic: [Solved] Widestring vs Ansistring  (Read 4598 times)

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
[Solved] Widestring vs Ansistring
« on: January 14, 2018, 08:56:43 pm »
I keep getting this Hint/warnig when i am compiling.
How can i prevent this?


I already tried conversions to widestring or ansistring and declaring the variable Code as either one of them.
« Last Edit: January 15, 2018, 09:04:17 pm by madref »
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: Widestring vs Ansistring
« Reply #1 on: January 14, 2018, 09:11:08 pm »
it most likely is warning you of possible loss of string integrity of data when you exchange the values
back and forth..

 If you obtain a WidnString value somewhere from a function and the pass it to a AnsiString, it most likely
will get converted to a UTF8 string and vice versa..

 if you are confident in what is happening then you could use a {%H-} just In front to have it ignore it.
The only true wisdom is knowing you know nothing

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Widestring vs Ansistring
« Reply #2 on: January 14, 2018, 09:36:19 pm »
I keep getting this Hint/warnig when i am compiling.
How can i prevent this?


I already tried conversions to widestring or ansistring and declaring the variable Code as either one of them.
create the smallest sample application possible that demostrates your problem and attach it here. Some one will spot the error for you.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Widestring vs Ansistring
« Reply #3 on: January 14, 2018, 11:02:57 pm »
Well you can make it your self
Code: Pascal  [Select][+][-]
  1. uses DCPrc4, DCPsha256;
  2.  
  3. var Code, smtpUser: string;
  4.  
  5.     function MyDecryption (const MC: string): string;
  6.     begin
  7.       Result := '';
  8.       if MC <> '' then
  9.         Result := Cipher.DecryptString(MC);
  10.     end;     // MyDecryption
  11.  
  12. begin
  13. ....
  14.       Cipher:= TDCP_rc4.Create(Self);
  15.       Cipher.InitStr(cfgFile,TDCP_sha256);         // initialize the cipher with a hash of the passphrase
  16.       Code := Form_RefereeMain.XMLConfig_DB.GetValue ('/RefereeDatabase/Setup/Base_UN/Value', '');
  17.       smtpUser := MyDecryption(Code);
  18. .....
  19. end;
  20.  


You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Widestring vs Ansistring
« Reply #4 on: January 14, 2018, 11:14:58 pm »
Well you can make it your self
Code: Pascal  [Select][+][-]
  1. uses DCPrc4, DCPsha256;
  2.  
  3. var Code, smtpUser: string;
  4.  
  5.     function MyDecryption (const MC: string): string;
  6.     begin
  7.       Result := '';
  8.       if MC <> '' then
  9.         Result := Cipher.DecryptString(MC);
  10.     end;     // MyDecryption
  11.  
  12. begin
  13. ....
  14.       Cipher:= TDCP_rc4.Create(Self);
  15.       Cipher.InitStr(cfgFile,TDCP_sha256);         // initialize the cipher with a hash of the passphrase
  16.       Code := Form_RefereeMain.XMLConfig_DB.GetValue ('/RefereeDatabase/Setup/Base_UN/Value', '');
  17.       smtpUser := MyDecryption(Code);
  18. .....
  19. end;
  20.  
sure but that does not show me your default settings
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Widestring vs Ansistring
« Reply #5 on: January 14, 2018, 11:26:47 pm »
I haven't changed the default settings ;)

You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: Widestring vs Ansistring
« Reply #6 on: January 15, 2018, 12:41:01 am »
I keep getting this Hint/warnig when i am compiling.
How can i prevent this?
I already tried conversions to widestring or ansistring and declaring the variable Code as either one of them.
May be this:
Code: Pascal  [Select][+][-]
  1. function Warning(const S: WideString): string;
  2. begin
  3.   Result := S;
  4. end;
  5.  
  6. function NoWarning(const S: WideString): string;
  7. begin
  8.   Result := UTF8Encode(S);
  9. end;

Thaddy

  • Hero Member
  • *****
  • Posts: 14210
  • Probably until I exterminate Putin.
Re: Widestring vs Ansistring
« Reply #7 on: January 15, 2018, 09:19:57 am »
That assumes string=UTF8, not string= AnsiString so that is technically not correct. What is correct is the warning, since the compiler can not guarantee potential data loss when converting to true Ansi.

Within the context of Lazarus apps (and provided LazUTF8 is used) it is correct though because that means string = utf8.
« Last Edit: January 15, 2018, 09:22:37 am by Thaddy »
Specialize a type, not a var.

circular

  • Hero Member
  • *****
  • Posts: 4196
    • Personal webpage
Re: Widestring vs Ansistring
« Reply #8 on: January 15, 2018, 10:52:35 am »
If you write explicitely AnsiString(...) for the type conversion, I suppose you will not get the warning.
Conscience is the debugger of the mind

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Widestring vs Ansistring
« Reply #9 on: January 15, 2018, 09:04:07 pm »
Thanks.... that solved it
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

 

TinyPortal © 2005-2018