今天有一个朋友找我让我帮忙写一个程序,要实现汉字国标码、机内码、区位码的相互转换
转换关系:
国际码=区位码(十六进制)+2020H
机内码=国际码+8080H
如此,我们就可以通过C#来实现这个程序
主要转换工作代码如下:
string jinei,guobiao,quwei;
int gb,qw;
//code[i / 2] = Convert.ToInt32(array[i], 16) * Convert.ToInt32(0x100, 16) + Convert.ToInt32(array[i+1], 16);
jinei = Convert.ToString(array[i], 16) + Convert.ToString(array[i + 1], 16);
gb = Convert.ToInt32(jinei, 16) - Convert.ToInt32("8080", 16);
guobiao = Convert.ToString(gb,16);
qw = gb - Convert.ToInt32("2020", 16);
quwei = Convert.ToString(qw, 16);
listBox1.Items.Add(textBox1.Text.Substring(i/2,1)+"的机内码是:"+jinei+",国标码是:"+guobiao+",区位码是:"+quwei);
全部代码:
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("请输入译码文字");
return;
}
int len;
len = textBox1.Text.Length;
byte[] array = System.Text.Encoding.Default.GetBytes(textBox1.Text);
string s="";
// int[] code = new int[len];
for (int i = 0; i < array.Length;i=i+2 )
{
string jinei,guobiao,quwei;
int gb,qw;
//code[i / 2] = Convert.ToInt32(array[i], 16) * Convert.ToInt32(0x100, 16) + Convert.ToInt32(array[i+1], 16);
jinei = Convert.ToString(array[i], 16) + Convert.ToString(array[i + 1], 16);
gb = Convert.ToInt32(jinei, 16) - Convert.ToInt32("8080", 16);
guobiao = Convert.ToString(gb,16);
qw = gb - Convert.ToInt32("2020", 16);
quwei = Convert.ToString(qw, 16);
listBox1.Items.Add(textBox1.Text.Substring(i/2,1)+"的机内码是:"+jinei+",国标码是:"+guobiao+",区位码是:"+quwei);
}
// MessageBox.Show(s);
}
}
程序提供下载: