Jump to content
dummzeuch

Communication between Unicode and non-Unicode applications

Recommended Posts

We currently are using TIdTCPClient and TIdTCPServer from Indy10 for communication between applications written in Delphi 2007, so all strings are AnsiStrings. Now I have got the first program that is written in Delphi 10.2, so all strings are now Unicode strings. Is there anything special I need to do so it can communicate with the existing server which is still written in Delphi 2007?

 

Edit: Strings here are only used for text (commands and parameters) not abused as buffer for binary data.

Edited by dummzeuch

Share this post


Link to post
Uses IdGlobal;

procedure TForm1.Button1Click(Sender: TObject);
var s: ansistring;
    t: TIdBytes;
begin
  s := AnsiString('ABCD');
  t := rawToBytes(s[1], length(s));
  IdTCPClient1.IOHandler.Write(t);
end;

To communicate with older devices, I use ansistrings like that.

Share this post


Link to post
1 hour ago, DelphiUdIT said:

Uses IdGlobal;

procedure TForm1.Button1Click(Sender: TObject);
var s: ansistring;
    t: TIdBytes;
begin
  s := AnsiString('ABCD');
  t := rawToBytes(s[1], length(s));
  IdTCPClient1.IOHandler.Write(t);
end;

To communicate with older devices, I use ansistrings like that.

You don't need to use RawToBytes() in this case.  The TIdIOHandler.Write(string) method has an optional ADestEncoding parameter to specify the byte encoding on the wire, such as UTF-8 (the default is ASCII).  You can alternatively use the TIdIOHandler.DefStringEncoding property.

 

And, in pre-Unicode versions, Write() also has an optional ASrcEncoding parameter to specify the byte encoding of the input AnsiString (the default is the user's OS default).  Or, you can alternatively use the TIdIOHandler.DefAnsiEncoding property.

Edited by Remy Lebeau
  • Thanks 2

Share this post


Link to post
2 hours ago, dummzeuch said:

Is there anything special I need to do so it can communicate with the existing server which is still written in Delphi 2007?

If you are using strictly ASCII characters only, then no.  All of the default settings should suffice.

 

But, if you are using any non-ASCII characters, then make sure both client and server agree on a common byte encoding on the wire, ie UTF-8.  You can use the TIdIOHandler.DefStringEncoding property, or the AByteEncoding parameter on individual read/write methods.  Indy will convert Unicode to wire encoding on writes, and from wire encoding to Unicode on reads.

 

In the D2007 code, you should also tell Indy which encoding your AnsiString's are using, if different than the OS default.  You can use the TIdIOHandler.DefAnsiEncoding property, or the ADestEncoding/ASrcEncoding parameter on individual read/write methods, respectively.    Indy will convert ANSI to Unicode to wire encoding on writes, and from wire encoding to Unicode to ANSI on reads.

Edited by Remy Lebeau
  • Thanks 1

Share this post


Link to post

Anyway, if your server is supportable and updatable, I'd recommend to throw away any ANSI (ACP, Def system codepage etc) stuff and just explicitly use UTF-8 and probably explicit plain ASCII for commands.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×