473,385 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

unicode strings and network byte ordering ?

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
5 3315
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael Weir | last post by:
I'm sure this is a very simple thing to do, once you know how to do it, but I am having no fun at all trying to write utf-8 strings to a unicode file. Does anyone have a couple of lines of code...
8
by: Eric Lilja | last post by:
Hello, I had what I thought was normal text-file and I needed to locate a string matching a certain pattern in that file and, if found, replace that string. I thought this would be simple but I had...
11
by: Patrick Van Esch | last post by:
Hello, I have the following problem of principle: in writing HTML pages containing ancient greek, there are two possibilities: one is to write the unicode characters directly (encoded as two...
4
by: webdev | last post by:
lo all, some of the questions i'll ask below have most certainly been discussed already, i just hope someone's kind enough to answer them again to help me out.. so i started a python 2.3...
15
by: John Salerno | last post by:
Forgive my newbieness, but I don't quite understand why Unicode is still something that needs special treatment in Python (and perhaps elsewhere). I'm reading Dive Into Python right now, and it...
14
by: Dennis Benzinger | last post by:
Hi! The following program in an UTF-8 encoded file: # -*- coding: UTF-8 -*- FIELDS = ("Fächer", ) FROZEN_FIELDS = frozenset(FIELDS) FIELDS_SET = set(FIELDS)
13
by: gabor | last post by:
hi, from the documentation (http://docs.python.org/lib/os-file-dir.html) for os.listdir: "On Windows NT/2k/XP and Unix, if path is a Unicode object, the result will be a list of Unicode...
18
by: Chameleon | last post by:
I am trying to #define this: #ifdef UNICODE_STRINGS #define UC16 L typedef wstring String; #else #define UC16 typedef string String; #endif ....
24
by: Donn Ingle | last post by:
Hello, I hope someone can illuminate this situation for me. Here's the nutshell: 1. On start I call locale.setlocale(locale.LC_ALL,''), the getlocale. 2. If this returns "C" or anything...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.