unicode strings and network byte ordering ?

G

Guest

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);

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 ?

Much Thanks,
Srikant
 
M

Morten Wennevik

Maybe System.Text.Encoding.BigEndianUnicode.GetBytes() is what you are
looking for.
 
J

Jon Skeet [C# MVP]

srikant said:
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);
 
G

Guest

Aha! I was going to do something altogether nasty, something along the lines of

int index = 0;
byte[] myBuffer = new byte[1024];
foreach (char c in myString)
{
byte[] hb = BitConverter.GetBytes(c);
short hs = BitConverter.ToInt16(hb, 0);
short ns = IPAddress.HostToNetworkOrder(hs);

byte[] nb = BitConverter.GetBytes(ns);
nb.CopyTo(myBuffer, index*2);
index++;
}

myNetworkStream.Write(myBuffer, 0, index*2);


Thanks for the pointer!!!
 
G

Guest

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 said:
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);
 
J

Jon Skeet [C# MVP]

srikant said:
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!!

You don't need to change the server - just use

Encoding unicodeEncoding = new UnicodeEncoding (true, false);

on the client.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top