Click here to Skip to main content
15,916,379 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to encode and decode the string into array of bytes using UTF-16LE?

I wamt to do the above using c# with asp.net
Posted
Comments
Milfje 5-Jun-15 5:48am    
UnicodeEncoding Class, method GetDecoder and GetEncoder: "Obtains an encoder that converts a sequence of Unicode characters into a UTF-16 encoded sequence of bytes.", and of course the reverse.

1 solution

Hope this helps.

XML
UnicodeEncoding unicode = new UnicodeEncoding();

// Create a string that contains Unicode characters.
String unicodeString = " (\u03a3).";

Response.Write("Original string:");
Response.Write(unicodeString);
Response.Write("<br />");
// Encode the string.
Byte[] encodedBytes = unicode.GetBytes(unicodeString);
Response.Write("<br />");
Response.Write("Encoded bytes:");
foreach (Byte b in encodedBytes)
{
    Response.Write(String.Format("[{0}]", b));
}
Response.Write("<br />");
// Decode bytes back to string.
// Notice Pi and Sigma characters are still present.
String decodedString = unicode.GetString(encodedBytes);
Response.Write("<br />");
Response.Write("Decoded bytes:");
Response.Write(decodedString);


OUTPUT AS -

Original string: (Σ).

Encoded bytes:[32][0][40][0][163][3][41][0][46][0]

Decoded bytes: (Σ)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900