user1390537
user1390537

Reputation: 15

Delphi XE2: UTF16LE -> UTF8

URL : 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] : working

[2] : not working (string broken > Shady: ??)

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

UTF8FromUTF16 function Original VB Code : VB 6.0 -> Delphi XE2 Conversion

Help me.. :(

Upvotes: 0

Views: 2943

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 598174

Strings are UTF-16 encoded in XE2. #2 does not work because you are passing a UTF8String where a UnicodeString is expected, so the RTL will perform a UTF-8 -> UTF-16 conversion before Indy sees the data, and then Indy will convert the UTF-16 data to its own default encoding during the transmission.

If you want to transmit a UnicodeString as UTF-8, you have to tell Indy to do that, via its TIdTextEncoding class, eg:

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; 

Or:

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; 

If you do not tell Indy which encoding to use, it will default to ASCII, which will corrupt/lose your Unicode data.

Upvotes: 3

Related Questions