近期用到這個函數,無奈沒有找到 delphi 自帶的,網上找了下 有類似的,沒有現成的,我需要的是 支持 “\u4f00 ” 這種格式的,即前面帶標准的 “\u” 於是改造了一下。
下面是 解碼 函數:
方便有需要的人吧,我自己也需要^_^
/// <summary>
/// // Unicode轉漢字 ,支持自動過濾非 unicode編碼,即非 unicode編碼不轉換
/// 只支持 標准的 類型 \u4e00 這種格式的 轉換, 以\u 開頭的
/// code by 猿哥哥 2015-2-11
/// </summary>
function TMyEncode.UnicodeToChinese(inputstr:string):string;
var
i:Integer;
index:Integer;
temp,top,last:string;
begin
index:=1;
while index>=0 do
begin
index:= inputstr.IndexOf('\u');
if index<0 then
begin
last:= inputstr;
Result:= Result+ last;
Exit;
end;
top:= Copy(inputstr,1,index); //取出 編碼字符前的 非 unic 編碼的字符,如數字
temp:= Copy(inputstr,index+1,6);//取出編碼,包括 \u ,如\u4e3f
Delete(temp,1,2);
Delete(inputstr,1,index+6);
result:= Result+ top+ WideChar( StrToInt('$'+ temp)) ;
end;
end;