问题
I would like to convert a CP-1253 string to Unicode and also perform the opposite conversion as well.
Suppose I have two variables holding the strings, a MySource1253
and a MyUnicodeTarget
.
I presume
AnsiString
to be the appropriate type forMySource1253
, whileString
should be suitable forMyUnicodeTarget
, please correct me if I am wrong.Is there some function in Delphi XE to make these conversions from one to the other and vice versa?
回答1:
Declare:
type
GreekString = type Ansistring(1253);
And to convert between them, just use following code:
var
UnicodeStr: string;
GreekStr: GreekString;
begin
UnicodeStr := 'This is a test.'; // Unicode string
GreekStr := GreekString(UnicodeStr); // ...converted to 1253
GreekStr := 'This is a test.'; // Greek string
UnicodeStr := string(GreekStr); // ...converted to Unicode
end;
See also: How can I convert string encoded with Windows Codepage 1251 to a Unicode string.
回答2:
Just call RawByteStringToUnicodeString and pass your AnsiString as a first argument and the code page (1253) as the second argument.
MyUnicodeString := RawByteStringToUnicodeString(MyAnsiString, 1253);
Here are the functions to convert from AnsiString(RawByteString) to Unicode and back. They are safe wrappers for the Win32 MultiByteToWideChar/WideCharToMultiByte.
uses
Windows, Math;
function RawByteStringToUnicodeString(const S: RawByteString; CP: Integer): UnicodeString;
var
P: PAnsiChar;
pw: PWideChar;
I, J: Integer;
begin
Result := '';
if S = '' then
Exit;
if CP = CP_UTF8 then
begin
// UTF8
Result := UTF8ToUnicodeString(S);
Exit;
end;
P := @S[1];
I := MultiByteToWideChar(CP, 0, P, Length(S), nil, 0);
if I <= 0 then
Exit;
SetLength(Result, I);
pw := @Result[1];
J := MultiByteToWideChar(CP, 0, P, Length(S), pw, I);
if I <> J then
SetLength(Result, Min(I, J));
end;
function UnicodeStringToRawByteString(const w: UnicodeString; CP: Integer): RawByteString;
var
P: PWideChar;
I, J: Integer;
begin
Result := '';
if w = '' then
Exit;
case CP of
CP_UTF8:
begin
// UTF8
Result := UTF8Encode(w);
Exit;
end;
CP_UNICODE_LE:
begin
// Unicode codepage
CP := CP_ACP;
end;
end;
P := @w[1];
I := WideCharToMultibyte(CP, 0, P, Length(w), nil, 0, nil, nil);
if I <= 0 then
Exit;
SetLength(Result, I);
J := WideCharToMultibyte(CP, 0, P, Length(w), @Result[1], I, nil, nil);
if I <> J then
SetLength(Result, Min(I, J));
SetCodePage(Result, CP, False);
end;
来源:https://stackoverflow.com/questions/40948221/conversion-of-a-string-from-some-codepage-to-unicode