// [url]http://www.delphipraxis.net/post644577.html#644577[/url]
function StringToWideStringEx(
const S:
string; CodePage: Word): WideString;
var
InputLength,
OutputLength: Integer;
begin
InputLength := Length(S);
OutputLength := MultiByteToWideChar(CodePage, 0, PChar(S), InputLength,
nil, 0);
SetLength(Result, OutputLength);
MultiByteToWideChar(CodePage, 0, PChar(S), InputLength, PWideChar(Result), OutputLength);
end;
function WideStringToStringEx(
const WS: WideString; CodePage: Word):
string;
var
InputLength,
OutputLength: Integer;
begin
InputLength := Length(WS);
OutputLength := WideCharToMultiByte(CodePage, 0, PWideChar(WS), InputLength,
nil, 0,
nil,
nil);
SetLength(Result, OutputLength);
WideCharToMultiByte(CodePage, 0, PWideChar(WS), InputLength, PChar(Result), OutputLength,
nil,
nil);
end;
function UTF8ToWideString(
const S:
string): WideString;
begin
Result := StringToWideStringEx(S, CP_UTF8);
end;
function WideStringToUTF8(
const WS: WideString):
string;
begin
Result := WideStringToStringEx(WS, CP_UTF8);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if Utf8Decode(Utf8Encode(widestring('
Täst'))) = '
Täst'
then
begin
showmessage('
OK');
// Wahr
end;
if widestring('
Testä') = UTF8ToWideString(WideStringToUTF8(widestring('
Testä')))
then
begin
showmessage('
OK');
// Wahr
end;
end;