Delphi 轉換 UTF8 字符爲 Unicode 字符


Function U8ToUnicode(szU8: PAnsiChar; var wszString: PWideChar): Integer;

Begin            //  UTF8 to Unicode
result := 0;
if szU8 = nil then exit;
wszString := Pointer(LocalAlloc(0, lstrlenA(szU8) * 2 + 1));
FillChar(wszString^, lstrlenA(szU8) * 2, 0);

Utf8ToUnicode(wszString, szU8, lstrlenA(szU8));

result := 1;

MessageBoxW(0, wszString, '已轉換', MB_OK)

end;



Function U8ToUnicode(szU8: PAnsiChar; var wszString: PWideChar): Integer;

Begin

result := 0;

if szU8 = nil then exit;
result := MultiByteToWideChar(CP_UTF8, 0, szU8, lstrlenA(szU8), nil, 0);
wszString := Pointer(LocalAlloc(0, result + 1));  //wchar_t*
//轉換
MultiByteToWideChar(CP_UTF8, 0, szU8, lstrlenA(szU8), wszString, result);
//加上'/0'
if (result > 0) and (result < (lstrlenW(wszString) + 1)) then LstrcatW(wszString, ''#0);
result := 1;
MessageBoxW(0, wszString, '已轉換', MB_OK)
end;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章