XE4 for ios 谨慎处理字符串

   因为xe4 for ios  里面的字符串处理有变化,具体能够参考官方文档,这两天帮一个朋友调试ios 的android

应用,因为没有注意这一块,折腾了很长时间。特此记录下来,但愿其余人不要走弯路。ios

如下面代码为例:函数

 

function myDecodestr(const AString:string):string;
const
   //                              0 1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
   Hex2Dec:array[0..31] of byte = (0,10,11,12,13,14,15,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0);
var
   i,k,l:integer;
    B: TBytes;
begin
     l:=Length(AString);
     if l<=0 then
     begin
          Result:='';
          exit;
     end;
     setlength(b,l);
     i:=1;
     k:=0;
      repeat
            if AString[i]='+' then
              begin
                   b[k]:=ord(' ');//  sb.Append(' ');
                   inc(i);
                   inc(k);
              end
              else if AString[i]='%' then
              begin
                  b[k]:=(Hex2Dec[ord(AString[i+1]) and $1F] shl 4)
                            +Hex2Dec[ord(AString[i+2]) and $1F];
                  inc(i,3);
                  inc(k);
              end
              else
              begin
                   b[k]:=ord(AString[i]);
                   inc(i);
                   inc(k);
              end;
        until i>l;
     setlength(b,k);

    result:=TEncoding.utf8.GetString(b);

end;

 

这个函数的功能就是把非标准ASCII 码进行编码,在win32 下,没有任何问题。编码

在ios 下,能够正经常运行,可是获得的结果不对。因为编译时也没有报错误,当时没有注意这一块,调试

在ios 上运行程序总是出错,通过跟踪才发现是win32 与 ios 下字符串处理的问题。code

IOS 上,已经不能使用 s[1], 这样表示字符串第一个字符了。并且也不建议使用s[i] 取字符串中的字符。blog

为了统一win32 与 IOS 下的代码(呵呵,也为后半年的android 作准备),以上代码使用XE4 的stringhelper进行开发

修改。文档

最后代码为:字符串

function myDecodestr(const AString:string):string;
const
   //                              0 1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
   Hex2Dec:array[0..31] of byte = (0,10,11,12,13,14,15,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0);
var
   i,k,l:integer;
    B: TBytes;
    c:char;
begin
     l:=Length(AString);
     if l<=0 then
     begin
          Result:='';
          exit;
     end;
     setlength(b,l);
     i:=0;
     k:=0;
     c:= AString.Chars[i];
      repeat
            if AString.Chars[i]='+' then
              begin
                   b[k]:=ord(' ');//  sb.Append(' ');
                   inc(i);
                   inc(k);
              end
              else if AString.Chars[i]='%' then
              begin
                  b[k]:=(Hex2Dec[ord(AString.Chars[i+1]) and $1F] shl 4)
                            +Hex2Dec[ord(AString.Chars[i+2]) and $1F];
                  inc(i,3);
                  inc(k);
              end
              else
              begin
                   b[k]:=ord(AString.Chars[i]);
                   inc(i);
                   inc(k);
              end;
        until i>l;
     setlength(b,k);

    result:=TEncoding.utf8.GetString(b);

end;

 注意,AString.Chars[i]  里面,第一个字符的i 为0,这与传统win32 的s[1] 为第一个字符不同。

在ios 下开发时特别要当心。

相关文章
相关标签/搜索