By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,154 Members | 1,037 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,154 IT Pros & Developers. It's quick & easy.

unicode strings and network byte ordering ?

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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 enabled
Nov 15 '05 #2

P: n/a
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
Nov 15 '05 #3

P: n/a
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!!!
Nov 15 '05 #4

P: n/a
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

Nov 15 '05 #5

P: n/a
srikant <an*******@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.