Einzelnen Beitrag anzeigen

Benutzerbild von sx2008
sx2008

Registriert seit: 15. Feb 2008
Ort: Baden-Württemberg
2.332 Beiträge
 
Delphi 2007 Professional
 
#3

AW: Delphi7 mit nur einer Unit Unicode fähig machen

  Alt 10. Jun 2013, 14:07
Interessant!

Für die Umwandlung von UTF8 <-> Widestring verwende ich immer die Windows API.
Als Zusatzbonus kann man neben UTF8 auch andere Codepages als Ziel oder Quelle angeben.
Delphi-Quellcode:
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;
fork me on Github
  Mit Zitat antworten Zitat