Connecting Tech Pros Worldwide Forums | Help | Site Map

unicode strings and network byte ordering ?

srikant
Guest
 
Posts: n/a
#1: Nov 15 '05
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

Morten Wennevik
Guest
 
Posts: n/a
#2: Nov 15 '05

re: unicode strings and network byte ordering ?


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
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#3: Nov 15 '05

re: unicode strings and network byte ordering ?


srikant <anonymous@discussions.microsoft.com> wrote:[color=blue]
> 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);[/color]

You can do almost exactly that:

byte[] buffer = Encoding.Unicode.GetBytes(str);
[color=blue]
> 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 ?[/color]

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
srikant
Guest
 
Posts: n/a
#4: Nov 15 '05

re: unicode strings and network byte ordering ?


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!!!
srikant
Guest
 
Posts: n/a
#5: Nov 15 '05

re: unicode strings and network byte ordering ?


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 <anonymous@discussions.microsoft.com> wrote:[color=blue]
> 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[color=green]
>> string = "hello world"; // this is a unicode strings (2 bytes per char)[/color]
> bytes[] buffer = UnicodeEncoding.GetBytes(str);
> myNetworkStream.Write(buffer, 0, buffer.Length);[/color]

You can do almost exactly that:

byte[] buffer = Encoding.Unicode.GetBytes(str);
[color=blue]
> 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 ?[/color]

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]
Guest
 
Posts: n/a
#6: Nov 15 '05

re: unicode strings and network byte ordering ?


srikant <anonymous@discussions.microsoft.com> wrote:[color=blue]
> 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!![/color]

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
If replying to the group, please do not mail me too
Closed Thread