如何使用UTF-16LE将字符串编码和解码为字节数组? [英] How to encode and decode the string into array of bytes using UTF-16LE?
本文介绍了如何使用UTF-16LE将字符串编码和解码为字节数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用UTF-16LE将字符串编码并解码为字节数组?
我想用asp.net
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
推荐答案
希望这会有所帮助。
Hope this helps.
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);
输出为 -
原始字符串:( Σ)。
编码字节:[32] [0] [40] [0] [163] [3] [41] [0] [46] [ 0]
解码字节:(Σ)
OUTPUT AS -
Original string: (Σ).
Encoded bytes:[32][0][40][0][163][3][41][0][46][0]
Decoded bytes: (Σ)
这篇关于如何使用UTF-16LE将字符串编码和解码为字节数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文