Groups | Blog | Home
all groups > c# > january 2004 >

c# : unicode strings and network byte ordering ?


srikant
1/22/2004 11:26:06 PM
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
1/22/2004 11:51:05 PM
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);


srikant
1/23/2004 12:06:05 AM
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: -----

[quoted text, click to view]

You can do almost exactly that:

byte[] buffer = Encoding.Unicode.GetBytes(str);

[quoted text, click to view]

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 - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jon Skeet [C# MVP]
1/23/2004 7:45:28 AM
[quoted text, click to view]

You can do almost exactly that:

byte[] buffer = Encoding.Unicode.GetBytes(str);

[quoted text, click to view]

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 - <skeet@pobox.com>
http://www.pobox.com/~skeet
Jon Skeet [C# MVP]
1/23/2004 8:25:24 AM
[quoted text, click to view]

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

Encoding unicodeEncoding = new UnicodeEncoding (true, false);

on the client.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Morten Wennevik
1/23/2004 8:33:12 AM
Maybe System.Text.Encoding.BigEndianUnicode.GetBytes() is what you are =

looking for.

-- =

Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
For a laugh, try web browsing with Opera's User Mode and Nostalgia enabl=
AddThis Social Bookmark Button