Hi Jon,
Thanks for your suggestions. .
I cannot at this point change the legacy server but I do know that the server converts each char back and forth from network order (kind of lame but I've got to live with it). Further, I don't even know the native endianness of the server, it could be Intel or Sparc, so I have to stick to network order for each char in the Unicode strings!!
Any more suggestions ?
Srikant
----- Jon Skeet [C# MVP] wrote: -----
srikant <an*******@discussions.microsoft.com> wrote:
I am writing a client in C# that needs to communicate over the network
to a legacy C++ application that uses Unicode strings. I realize that
C# strings are already in Unicode, however, how do I account for the
network order transformation. Can I simply do the equivalent of string = "hello world"; // this is a unicode strings (2 bytes per char) bytes[] buffer = UnicodeEncoding.GetBytes(str);
myNetworkStream.Write(buffer, 0, buffer.Length);
You can do almost exactly that:
byte[] buffer = Encoding.Unicode.GetBytes(str);
I guess, if the client and server are both on the same architecture
then this isn't an issue. However, what if my legacy server is on Unix
(Solaris) ? Won't the byte ordering/endianness create problems ?
It shouldn't, for two reasons:
1) Endianness of the machine has very little, if anything, to do with
the endianness of the encoding. You can use a "big endian" encoding on
a little endian machine, or vice versa.
2) If you want to make things clear to the other end of the stream,
start off with a BOM (byte order mark) which the other end can use to
work out which endianness you're using.
In your situation though, it would be best to find out which endianness
the C++ application is expecting and use either:
Encoding unicodeEncoding = new UnicodeEncoding (true, false);
or
Encoding unicodeEncoding = new UnicodeEncoding (false, false);
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too