Search code examples
delphiencryptiondelphi-7delphi-xe

Delphi 10: What can I do about String[25] changing some char values to "?"


I have ported a Delphi 7.1 application to Delphi 10.3. I have some simple encrypting/decryption functions. And if I encrypt string values and encrypt them, everything is fine:

var
   test, encrypted, decrypted : string;
begin
  test := 'XXXXXXXX'; // hidden message 
  encrypted := _common.encrypt(test);
  decrypted := _common.decrypt(encrypted );  
end;

in this scenario, everything works as expected, even with special characters, encrypted would be: 'y'#$0080'vn'

but if the value is of string[25], it handles special characters differently:

var
   test,decrypted : string;
   encrypted : string[25]
begin
  test := 'XXXXXXXX'; // hidden message 
  encrypted := _common.encrypt(test);
  decrypted := _common.decrypt(encrypted);  
end;

in this scenario, everything work as expected unless the encrypted string contains special characters in this example res1 would be: 'y?vn'

I'm using string[] in records, when writing/reading data to/from disk

How can I fix this?

Can I use a different string type for the record type, or ?

/Flemming


Solution

  • Since Delphi 7, the string type has changed from one-byte ANSI characters to two-byte Unicode characters. However, the fixed-length string[n] still is a one-byte ANSI string. Therefore, you are mixing different string types. The easiest fix might be to switch those variables which you declare as string to a declaration as AnsiString instead.