tags:

views:

155

answers:

2

I converting a lecacy app from Delphi 7 to Delphi 2009. I got this error: E2010 Incompatible types: 'Char' and 'AnsiChar' How can I fix it ? I tried to declare Alphabet: Ansistring[AlphabetLength] but that failed.

const
  AlphabetLength = 64;
  Alphabet: string[AlphabetLength] = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';

function TBase64.ValueToCharacter(value: Byte; var character: char): boolean;
begin
  Result := true;
  if (value > AlphabetLength-1) then
    Result := false
  else
// Compile error E2010 Incompatible types: 'Char' and 'AnsiChar'
    character := Alphabet[value+1];
end;    

function TBase64.CharacterToValue(character: char; var value: byte): boolean;
begin
  Result := true;
  value := Pos(character, Alphabet);
  if value = 0 then
    Result := false
  else
    value := value-1;
end;
A: 

You could just use

Alphabet : String = 'ABCDEFGH...'?

Or if you are sure that you only use ANSI characters (which seems to be the case here), you can just cast:

character := Char (Alphabet [value+1]);

It seems that the short string type is using AnsiChar internally even in D2009 and higher.

Smasher
ShortString not only seems to be it is definitely an "array of AnsiChar". ShortString is inherited from TurboPascal and the internal layout limits it to max. 255 characters.
Andreas Hausladen
+3  A: 

Avoid using the deprecated ShortString type in Unicode Delphi versions (2009 and later):

const
  AlphabetLength = 64;
  Alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';

the above change should be enough. You must also think about changing from 1-byte AnsiChars to 2-byte Chars.

edit (jeroen pluimers):

Here is some documentation on the string types.

Serg
ShortString is deprecated only in Delphi.NET as far as I know.
Deltics
+1; I edited your answer to point to some string types documentation.
Jeroen Pluimers
Great, I need much help :)
Roland Bengtsson