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

  • Thread starter Thread starter qushui_chen
  • Start date Start date
Q

qushui_chen

I store the MSSqlServer is Nvarchar(unicode),
the store data as "我就是我文本",
How can i decode in C#?
 
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 said:
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 :)
 
the store data as "我就是我文本",
How can i decode in C#?

another solution:

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