我在delphi XE8中很新。我有以下代码是从我的delphi版本6,我想在delphi XE8运行它。不兼容的类型:'PAnsiChar'和'PWideChar'
1. function UTF8ToStringLen(const src: PChar; const Len: Cardinal): widestring;
2. var
3. l: Integer;
4. begin
5. SetLength(Result, Len);
6. if Len > 0 then
7. begin
8. l := MultiByteToWideChar(CP_UTF8, 0, src, Len, PWChar(Result), Len*SizeOf(WideChar)); <--error
9. SetLength(Result, l);
10. end;
11. end;
12.
13.
14. function StringToUTF8Len(const src: PChar; const Len: Cardinal): string;
15. var
16. bsiz: Integer;
17. Temp: string;
18. begin
19. bsiz := Len * 3;
20. SetLength(Temp, bsiz);
21. if bsiz > 0 then
22. begin
23. StringToWideChar(src, PWideChar(Temp), bsiz);
24. SetLength(Result, bsiz);
25. bsiz := WideCharToMultiByte(CP_UTF8, 0, PWideChar(Temp), -1, PChar(Result), bsiz, nil, nil); <--error
26. if bsiz > 0 then dec(bsiz);
27. SetLength(Result, bsiz);
28. end;
29. end;
,当我尝试运行它错误指向8号线和25中的代码有一个错误信息说
ERROR MESSAGE: Incompatible types: 'PAnsiChar' and 'PWideChar'
我搜索无处不在的解决方案,但我只是不能解决问题。请帮助..谢谢。
由于XE8支持Unicode,因此PChar与PWideChar类似。解决你的问题申报src:PAnsiChar。不过,你可能还有更多要做。我建议阅读MarcoCantú关于Unicode的白皮书:http://edn.embarcadero.com/article/38980 –