470,601 Members | 1,527 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,601 developers. It's quick & easy.

How can I decode the unicode?? that is very exigency

I store the MSSqlServer is Nvarchar(unicode),
the store data as "我就是我文本",
How can i decode in C#?
Nov 15 '05 #1
3 7973
Hi,

First parse the string to extract the numbers themselves and store them in
an array of ints. Then split each number to a couple of bytes (the high byte
is obtained as

(theNumber & 0xff00) >> 8

and the low one as

(theNumber & 0xff)

The resultant bytes should be stored in a byte array with capacity equal to
doubled number of character codes.

The low bytes should be stored first (i.e. in even indexes - 0, 2, 4) and
the high bytes second (in odd indexes - 1, 3, 5)

When the byte array is ready, use code like this:

string result = System.Text.Encoding.Unicode.GetString(theBytes);

P.S. If you store your data in a MS SQL database, why HTML encode them in
the database? Wouldn't it be more reasonable to store the data as unicode
text in the database and HTML-encode it only when the data are rendered to a
Web page?

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Unit Testing and Integration Environment
http://x-unity.miik.com.ua
Deliver reliable .NET software

"qushui_chen" <qu*********@asus.com.cn> wrote in message
news:OF**************@TK2MSFTNGP09.phx.gbl...
I store the MSSqlServer is Nvarchar(unicode),
the store data as "我就是我文本",
How can i decode in C#?


Nov 15 '05 #2
Dmitriy Lapshin [C# / .NET MVP] <x-****@no-spam-please.hotpop.com>
wrote:
First parse the string to extract the numbers themselves and store them in
an array of ints. Then split each number to a couple of bytes (the high byte
is obtained as

(theNumber & 0xff00) >> 8

and the low one as

(theNumber & 0xff)

The resultant bytes should be stored in a byte array with capacity equal to
doubled number of character codes.

The low bytes should be stored first (i.e. in even indexes - 0, 2, 4) and
the high bytes second (in odd indexes - 1, 3, 5)

When the byte array is ready, use code like this:

string result = System.Text.Encoding.Unicode.GetString(theBytes);


That sounds like a lot of unnecessary work. Once you've parsed the
numbers as their 16 bit unicode values, just create a char array of the
right size, and set each char to be the appropriate value as parsed.
Then use the String(char[]) constructor. No need to split 16 bit
numbers into 2 bytes and then just recombine them :)

--
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
> the store data as "我就是我文本",
How can i decode in C#?


another solution:

string x = System.Web.HttpUtility.HtmlDecode( "我就是我文本" );


--
Thomas Scheidegger - MVP .NET - 'NETMaster'
http://www.cetus-links.org/oo_dotnet.html - http://dnetmaster.net/
Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by David Pratt | last post: by
15 posts views Thread by glacier | last post: by
1 post views Thread by Eric S. Johansson | last post: by
2 posts views Thread by Gilles Ganault | last post: by
1 post views Thread by anonymous | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.