因爲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 下開發時特別要當心。