私はMultiByteToWideCharを使用しようとしていますが、私は '宣言されていない識別子'を取得します。それはどこに宣言されていますか?それは「使う」ものですか?delphiでMultiByteToWideCharを使用するには?
私はEmbarcadero Delphi XE8を使用しています。
私はMultiByteToWideCharを使用しようとしていますが、私は '宣言されていない識別子'を取得します。それはどこに宣言されていますか?それは「使う」ものですか?delphiでMultiByteToWideCharを使用するには?
私はEmbarcadero Delphi XE8を使用しています。
これはWindowsユニットで定義されています。 uses節にWindows
を追加するだけです。
uses
Windows;
function StringToWideStringCP(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 WideStringToStringCP(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;
これはWindows API関数なので、呼び出すにはWinapi.Windows
を使用する必要があります。
クロスプラットフォームのコードを書く場合は、UnicodeFromLocaleChars
としてください。
しかし、POSIXプラットフォーム(macOS、iOS、おそらくAndroid)でも、 'UnicodeFromLocaleChars'はssslllooowwwです。私の 'Mobile.AnsiString'ユニット(AnsiStringがモバイルコンパイラで利用できないときに書かれたもの)では、10倍以上高速化できました。 Windowsでは高速ですが、完全には正しくありません(内部で使用する 'MultiByteToWideChar'もありません)。 –
使用[UnicodeFromLocaleChars(http://docwiki.embarcadero.com/Libraries/XE8/en/System.UnicodeFromLocaleChars)。 – Victoria