0

网址:http ://www.gagalive.kr/livechat1.swf?chatroom=~~~BBQ

[1]---------------------------------------------------------- --------------------

procedure TForm1.FormCreate(Sender: TObject);

begin

 IdTCPClient.Host := '61.97.246.131';

  IdTCPClient.Port := 8080;

  IdTCPClient.Connect;

  IdTCPClient.IOHandler.Write('Ag8m' + Char(0));

  IdTCPClient.IOHandler.Write('LShady|###BBQ' + Char(0));

  IdTCPClient.IOHandler.Write('#' + 'Some Text'  + Char(0));

  IdTCPClient.Disconnect;

end;

[2]---------------------------------------------------------- --------------------

function UTF8FromUTF16_2(sUTF16: UnicodeString): UTF8String; 

begin   

  Result := sUTF16; 

end; 

procedure TForm1.FormCreate(Sender: TObject);

begin

 IdTCPClient.Host := '61.97.246.131';

  IdTCPClient.Port := 8080;

  IdTCPClient.Connect;

  IdTCPClient.IOHandler.Write('Ag8m' + Char(0));

  IdTCPClient.IOHandler.Write('LShady|###BBQ' + Char(0));

  IdTCPClient.IOHandler.Write(UTF8FromUTF16_2('#' + '안녕'  + Char(0)));

  IdTCPClient.Disconnect;

end;

[1]:工作

[2]:不工作(断线>黑幕:??)

见:http ://www.gagalive.kr/livechat1.swf?chatroom=~~~BBQ

UTF8FromUTF16 函数原始 VB 代码:VB 6.0 -> Delphi XE2 转换

帮我.. :(

4

1 回答 1

3

字符串在 XE2 中以 UTF-16 编码。#2 不起作用,因为您传递的UTF8StringUnicodeString预期的 a,因此 RTL 将在 Indy 看到数据之前执行 UTF-8 -> UTF-16 转换,然后 Indy 会将 UTF-16 数据转换为自己的默认值传输过程中的编码。

如果你想以UnicodeStringUTF-8 格式传输 a,你必须通过它的TIdTextEncoding类告诉 Indy 这样做,例如:

procedure TForm1.FormCreate(Sender: TObject); 
begin 
  IdTCPClient.Host := '61.97.246.131'; 
  IdTCPClient.Port := 8080; 
  IdTCPClient.Connect; 
  IdTCPClient.IOHandler.Write('Ag8m'#0); 
  IdTCPClient.IOHandler.Write('LShady|###BBQ'#0); 
  IdTCPClient.IOHandler.Write('#안녕'#0, TIdTextEncoding.UTF8); // <-- here
  IdTCPClient.Disconnect; 
end; 

或者:

procedure TForm1.FormCreate(Sender: TObject); 
begin 
  IdTCPClient.Host := '61.97.246.131'; 
  IdTCPClient.Port := 8080; 
  IdTCPClient.Connect; 
  IdTCPClient.IOHandler.DefStringEncoding := TIdTextEncoding.UTF8; // <-- here
  IdTCPClient.IOHandler.Write('Ag8m'#0); 
  IdTCPClient.IOHandler.Write('LShady|###BBQ'#0); 
  IdTCPClient.IOHandler.Write('#안녕'#0);
  IdTCPClient.Disconnect; 
end; 

如果您不告诉 Indy 使用哪种编码,它将默认为 ASCII,这将损坏/丢失您的 Unicode 数据。

于 2012-05-15T00:02:51.437 回答