HTTP経由で通信するサーバーとモバイル クライアントを開発しています。サーバーは Delphi 7 で作成され(古いコードとの互換性が必要なため)、クライアントは XE6 で作成されたモバイル アプリケーションです。サーバーは、文字列を含むデータのストリームをクライアントに送信します。エンコーディングに問題があります。
サーバー上で文字列をUTF8で渡そうとします:
//Writes string to stream
procedure TStreamWrap.WriteString(Value: string);
var
BytesCount: Longint;
UTF8: string;
begin
UTF8 := AnsiToUtf8(Value);
BytesCount := Length(UTF8);
WriteLongint(BytesCount); //It writes Longint to FStream: TStream
if BytesCount > 0 then
FStream.WriteBuffer(UTF8[1], BytesCount);
end;
Delphi7 で書かれているように、Value は 1 バイト文字列です。
クライアントで文字列をUTF8で読み取り、それをUnicodeにエンコードします
//Reads string from current position of stream
function TStreamWrap.ReadString: string;
var
BytesCount: Longint;
UTF8: String;
begin
BytesCount := ReadLongint;
if BytesCount = 0 then
Result := ''
else
begin
SetLength(UTF8, BytesCount);
FStream.Read(Pointer(UTF8)^, BytesCount);
Result := UTF8ToUnicodeString(UTF8);
end;
end;
しかしShowMessage
、文字が間違っている文字列を表示するとうまくいきません。では、Delphi 7 に文字列を保存し、モバイル アプリの XE6 に復元するにはどうすればよいでしょうか。文字列を表すデータの先頭にBOMを追加する必要がありますか?