开发者

Conversion for Delphi 2009 unicode issue

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 ty开发者_开发问答pes: '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;


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜