delphi XE4 for ios 谨慎处理字符串


本文整理自网络,侵删。

 由于xe4 for ios  里面的字符串处理有变化,具体可以参考官方文档,这两天帮一个朋友调试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 下字符串处理的问题。

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

为了统一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 下开发时特别要小心。

相关阅读 >>

Delphi写的dll回调c#

Delphi 键盘记录源代码

Delphi 截取某个字符之前的字段

Delphi pos的用法

Delphi 创建快捷方式的单元文件

Delphi idhttpserver实现webservice

Delphi webbrowser通过遍历id查找元素是否存在

Delphi 合并字符串的函数

Delphi android / ios应用程序中使用tgeocoder类进行反向地理编码(从位置信息中获取地址)

移植Delphi7的tclientsocket,tserversocket

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...