请问c#中如何将字符转换成ASCII码,包括汉字
解决方案 »
- 为什么DataView的刷选不起作用
- Winform树形菜单和文件读写
- SendMessage只能对当前活动窗口执行吗?
- 怎么让IDE给代码自动加上分隔线?
- 拖动窗体上的绘图
- 昨日面试逻辑题:6,7,3,0,3,6,9,? 在google上都没搜到答案
- 正则表达式字符串替换,请高手指点!!!
- 请问用c#如何读取mp3文件的信息啊(比如作者信息)谢谢
- 用sql server数据库建表时的问题
- 为什么不是追加写入到excel
- 我自己写了一个简单的Windows的控件是一个画圆的小控件,可是放到Winform上后,当改变控件大小时,里面画的圆却不能即时刷新改变(是根据
- textBox 简单绑定的问题
{
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());
}
}
}
//怎么转都行
byte[] sby =System.Text.Encoding.UTF8.GetBytes("字符串");
for(int i=0;i<sby.Length;i++)
{
str+=(string)sby[i];
}
MessageBox.Show(str);
int nAsc=0;
foreach(char c in str)
nAsc=c;
char c = 'a';
int i = (int)c;
汉字没有ascii对应,可以使用unicode 或者utf8
string str="";
for(int i=0;i<sby.Length;i++)
{
str+=sby[i].ToString();
}
this.Label1.Text = str.ToString();