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 32 33 34 35 36 37 38 39 40 41 42 43 44
|
function AnsiToUnicode(const str: AnsiString; codePage: Integer): UnicodeString;
var
len: Integer;
begin
{$IFDEF UNICODE}
if codePage = 0 then codePage := DefaultSystemCodePage;
if codePage = 0 then codePage := StringCodePage(str);
DefaultSystemCodePage := codePage;
{$ENDIF}
if str = '' then Result := ''
else
begin
len := MultiByteToWideChar(codePage, 0, PAnsiChar(str), -1, nil, 0);
if len > 1 then
begin
SetLength(Result, len - 1);
MultiByteToWideChar(codePage, 0, PAnsiChar(str), -1, PWideChar(Result), len);
end
else Result := '';
end;
end;
function UnicodeToAnsi(const str: UnicodeString; codePage: Integer): AnsiString;
var
len: Integer;
begin
{$IFDEF UNICODE}
if codePage = 0 then codePage := DefaultSystemCodePage;
if codePage = 0 then codePage := StringCodePage(str);
DefaultSystemCodePage := codePage;
{$ENDIF}
if str = '' then Result := ''
else
begin
len := WideCharToMultiByte(codePage, 0, PWideChar(str), -1, nil, 0, nil, nil);
if len > 1 then
begin
SetLength(Result, len - 1);
WideCharToMultiByte(codePage, 0, PWideChar(str), -1, PAnsiChar(Result), len, nil, nil);
end
else Result := '';
end;
end; |
Partager