请问c#中如何将字符转换成ASCII码,包括汉字

解决方案 »

  1.   

    namespace A.UCode
    {
      using System;
      using System.IO;
      using System.Windows.Forms;
      using System.Drawing;
      using System.Text;  // Encoding代码查看
      class UCodeForm : Form
      {
        TextBox tbxScode, tbxUcode, tbxAcode;
        Button btnOK, btnSave;
        ComboBox cbScode;
        string []keystr;
        Encoding []objEncoding;
        byte [] bytes;    // 构造必要的数据
        void MkSource()
        {
         keystr = new string[]
         {
           "Default",
           "Unicode",
           "UTF7",
           "UTF8",
           "ASCII",
           "BigEndianUnicode"
         };
          objEncoding = new Encoding []
          {
            Encoding.Default,
            Encoding.Unicode,
            Encoding.UTF7,
            Encoding.UTF8,
            Encoding.ASCII,
            Encoding.BigEndianUnicode
          };
        }    public UCodeForm()
        {
          MkSource();
          cbScode = new ComboBox();
          cbScode.Location = new Point(10, 10);
          cbScode.Size = new Size(120, 25);
          cbScode.DropDownStyle = ComboBoxStyle.DropDownList;
          cbScode.DataSource = keystr;      btnOK = new Button();
          btnOK.Text = "OK";
          btnOK.Location = new Point(140, 10);
          btnOK.Size = new Size(30, 22);
          btnOK.Click += new EventHandler(OK_Clicked);      btnSave = new Button();
          btnSave.Text = "保存(&S)...";
          btnSave.Location = new Point(210, 10);
          btnSave.Size = new Size(80, 22);
          btnSave.Click += new EventHandler(Save_Clicked);      tbxScode = new TextBox();
          tbxScode.Location = new Point(10, 40);
          tbxScode.Size = new Size(300, 35);
          tbxScode.Multiline = true;      tbxUcode = new TextBox();
          tbxUcode.Location = new Point(10, 85);
          tbxUcode.Size = new Size(300, 100);
          tbxUcode.Multiline = true;
          tbxUcode.ReadOnly = true;      tbxAcode = new TextBox();
          tbxAcode.Location = new Point(10, 195);
          tbxAcode.Size = new Size(300, 35);
          tbxAcode.Multiline = true;
          tbxAcode.ReadOnly = true;      Controls.AddRange(new Control[]{ cbScode, tbxScode, tbxUcode, tbxAcode, btnOK, btnSave });
          FormBorderStyle = FormBorderStyle.FixedDialog;
          MaximizeBox = false;
          ClientSize = new Size(320, 240);
          this.StartPosition = FormStartPosition.CenterScreen;
          this.FormBorderStyle = FormBorderStyle.Fixed3D;
          this.WindowState = FormWindowState.Normal;
          this.Text = string.Format("Encoding代码查看工具(C#)");
        }    // 转换
        private void OK_Clicked(object sender, System.EventArgs e)
        {
          string s = "", sA= "";
          bytes = objEncoding[cbScode.SelectedIndex].GetBytes(tbxScode.Text);
          int i = 0;
          foreach(byte b in bytes)
          {
            i++;
            s  += string.Format("{0:X2} ", (int)b);
            sA += (char)b;
          }
          tbxUcode.Text = s.Trim();
          tbxAcode.Text = sA;
        }    // 保存
        private void Save_Clicked(object sender, System.EventArgs e)
        {
          SaveFileDialog sfd = new SaveFileDialog();
          sfd.DefaultExt = "bin";       // "ini"
          sfd.Filter     = "bin files|*.bin|All files|*.*";    // "bin files|*.bin|All files|*.*";
          sfd.Title      = "保存BIN数据到文件";     // "保存文件";
          sfd.InitialDirectory = ".";
          if (sfd.ShowDialog() != DialogResult.OK) return;      // 取消
          FileStream fs = null;
          try
          {
            fs = new FileStream(sfd.FileName, FileMode.Create);
            foreach(byte b in bytes) fs.WriteByte(b);
          }
          catch(Exception err)
          {
            MessageBox.Show(err.Message, "打开文件出错");
          }
          finally
          {
            fs.Close();
          }
        }    // 入口
        [STAThread]
        static void Main()
        {
          Application.Run(new UCodeForm());
        }
      }
    }
    //怎么转都行
      

  2.   

    char chr='a';(short)chr //就是Asc码
      

  3.   

    byte[] byr=System.Text.ASCII.GetBytes("字符串")
      

  4.   

    不好意思,写错了,应该是:
    byte[] sby =System.Text.Encoding.UTF8.GetBytes("字符串");
      

  5.   

    string str="";
    for(int i=0;i<sby.Length;i++)
    {
       str+=(string)sby[i];
    }
    MessageBox.Show(str);
      

  6.   

    刚也用了一下,好使。不是UTF8用Default属性。byte[] sby =System.Text.Encoding.Default.GetBytes("字符串");
      

  7.   

    用byte[] array = System.Text.Encoding.Default.GetBytes("南京");就可以
      

  8.   

    最简单的办法:
    int nAsc=0;
    foreach(char c in str)
     nAsc=c;
      

  9.   

    字符转换成ASCII码: 
    char c  = 'a';
     int i = (int)c;
    汉字没有ascii对应,可以使用unicode 或者utf8
      

  10.   

    byte[] sby =System.Text.Encoding.UTF8.GetBytes("a");
    string str="";
    for(int i=0;i<sby.Length;i++)
    {
    str+=sby[i].ToString();
    }
    this.Label1.Text = str.ToString();