Вопрос

I have a strange problem.

I'm using Delphi 2007 and running it with the -r switch. On my computer everything works fine. When I transfer code to another computer I get an error:

Incompatible types char and widechar.

Maybe I should change some options.

Function that makes the problem:

function THcp.ConVertString(s: string): string;
Var i:integer;
    lstr:string;
begin
     lstr:=EmptyStr;
     for i := 1 to Length(s) do
     begin
           case s[i] of
             'Č': s[i]:='C';
             'č': s[i]:='c';
             'Ć': s[i]:='C';
             'ć': s[i]:='c';
             'Š': s[i]:='S';
             'š': s[i]:='s';
             'Đ': s[i]:='D';
             'đ': s[i]:='d';
             'Ž': s[i]:='Z';
             'ž': s[i]:='z';
           end;
           lstr:=lstr+s[i];
     end;
     Result:=lstr;
end;
Это было полезно?

Решение

This is my hypothesis. On the machine on which the code compiles, the non-ASCII characters in the code are all valid ANSI characters for that machine's locale. But the other machine uses a different locale under which some of those characters are not included in the >= 128 portion of the codepage. And hence those characters are promoted to WideChar and so of course are not compatible with AnsiChar.

Другие советы

The reason for this could very much be the reason David suggests.

If you declare the function like this:

function THcp.ConVertString(s: AnisString): AnsiString;

Then that reason yet only applies for the character constants in your code, not for the input. By elimination of those constants by using the character order instead, like I once did in these routines, then I suppose this will compile.

function AsciiExtToBase(Index: Byte): Byte; overload;
const
  Convert: array[128..255] of Byte = (
  //128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
  //  €       ‚   ƒ   „   …   †   ‡   ˆ   ‰   Š   ‹   Œ       Ž           ‘   ’
  //  E       ,   f   "               ^       S   <           Z           '   '
     69,129, 44,102, 34,133,134,135, 94,137, 83, 60,140,141, 90,143,144, 41, 41,
  //147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
  //  “   ”   •   –   —   ˜   ™   š   ›   œ       ž   Ÿ       ¡   ¢   £   ¤   ¥
  //  "   "       -       ~       s   >           z   Y       !
     34, 34,149, 45,151,126,153,115, 62,156,157,122, 89,160, 33,162,163,164,165,
  //166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
  //  ¦   §   ¨   ©   ª   «   ¬   *   ®   ¯   °   ±   ²   ³   ´   µ   ¶   ·   ¸
  //  |           c   a   <       -                   2   3   '
    124,167,168, 99, 97, 60,172, 45,174,175,176,177, 50, 51, 41,181,182,183,184,
  //185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
  //  ¹   º   »   ¼   ½   ¾   ¿   À   Á   Â   Ã   Ä   Å   Æ   Ç   È   É   Ê   Ë
  //  1       >               ?   A   A   A   A   A   A   A   C   E   E   E   E
     49,186, 62,188,189,190, 63, 65, 65, 65, 65, 65, 65, 65, 67, 69, 69, 69, 69,
  //204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
  //  Ì   Í   Î   Ï   Ð   Ñ   Ò   Ó   Ô   Õ   Ö   ×   Ø   Ù   Ú   Û   Ü   Ý   Þ
  //  I   I   I   I   D   N   O   O   O   O   O   x       U   U   U   U   Y
     73, 73, 73, 73, 68, 78, 79, 79, 79, 79, 79,120,216, 85, 85, 85, 85, 89,222,
  //223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
  //  ß   à   á   â   ã   ä   å   æ   ç   è   é   ê   ë   ì   í   î   ï   ð   ñ
  //      a   a   a   a   a   a   a   c   e   e   e   e   i   i   i   i   o   n
    223, 97, 97, 97, 97, 97, 97, 97, 99,101,101,101,101,105,105,105,105,111,110,
  //242 243 244 245 246 247 248 249 250 251 252 253 254 255
  //  ò   ó   ô   õ   ö   ÷   ø   ù   ú   û   ü   ý   þ   ÿ
  //  o   o   o   o   o   /       u   u   u   u   y       y
    111,111,111,111,111, 47,248,117,117,117,117,121,254,121);
begin
  if Index < 128 then
    Result := Index
  else
    Result := Convert[Index];
end;

function AsciiExtToBase(AChar: AnsiChar): AnsiChar; overload;
begin
  Result := Chr(AsciiExtToBase(Ord(AChar)));
end;

function AsciiExtToBase(const S: AnsiString): AnsiString; overload;
var
  P: PByte;
  I: Integer;
begin
  Result := S;
  P := @Result[1];
  for I := 1 to Length(Result) do
  begin
    P^ := AsciiExtToBase(P^);
    Inc(P);
  end;
end;
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top