webform1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace mustdo_002
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//关于字符编码的问题
string str = "M国"; // c#默认Unicode编码格式
//获取Unicode编码
System.Text.Encoding uniStr = System.Text.Encoding.Unicode;
byte[] uniBytes = uniStr.GetBytes(str);
Page.Response.Write("Unicode:");
for (int i = 0; i < uniBytes.Length; i++)
{
Page.Response.Write(uniBytes[i] + "-");
}
Page.Response.Write(">" + uniStr.GetString(uniBytes)+ "<br>");
System.Text.Encoding asciiStr = System.Text.Encoding.ASCII;
//获取ASCII编码
byte[] asciiBytes = asciiStr.GetBytes(str);
Page.Response.Write("ASCII:");
for (int j = 0; j < asciiBytes.Length; j++)
{
Page.Response.Write(asciiBytes[j] + "-");
}
Page.Response.Write(">" + asciiStr.GetString(asciiBytes) + "<br>");
/*
* 字符集范围由小到大,依次为ASCII,扩展ASCII,MBCS字符集,Unicode;ASCII一般适合西方国家表示英文字符集,我国汉字较多,需要更大
* 范围的字符集,Unicode使用两个字节表示一个字符,共6万多个字符,包括英文字母和方块字
*
*/
}
}
}