一个按钮:button1
2个文本框:WebName    txt_read_webname我要实现:点击按钮,WebName文本框内的值转换为ASCII码,然后把ASCII赋给txt_read_webname文本框
 private void button1_Click(object sender, EventArgs e)
        {
                      
            
            string webname = WebName.Text.Trim();//取文本框 内的非空格值赋值给字符串
                        
            byte[] pucWriteBuf = new byte[60];//定义一个BYTE
            pucWriteBuf = System.Text.Encoding.ASCII.GetBytes(webname);//转换webname的值为ASCII码赋给BYTE的pucWriteBuf              txt_read_webname.text=pucWriteBuf ;  // 这句话是错误的!具体怎么写》?还有不知道其他地方是否错误!希望根据我要实现的目的给予纠正·谢谢大家
}

解决方案 »

  1.   

     楼主不能把byte数组赋值给text ,text是string类型的。
    你需要遍历bytes数组,对每个元素进行tostring,然后拼接起来,就得到你要的结果了。出来的是类似9798之类的ascii码
      

  2.   

    byte[] pucWriteBuf = System.Text.Encoding.ASCII.GetBytes(webname);            string x = "";
                for(int i=0;i<pucWriteBuf.Length;i++)
                {
                    x += string.Format("{0:x} ", pucWriteBuf[i]);
                }
      

  3.   

    哥 数组不能当string用,转成string赋给Text属性
      

  4.   

    pucWriteBuf = System.Text.Encoding.ASCII.GetBytes(webname);//转换webname的值为ASCII码赋给BYTE的pucWriteBuf 
    string str = "";
    for(int i = 0;i< pucWriteBuf.Length;i++)
    {
        str += bt[i] & 0xFF;
    }
                 txt_read_webname.text=str ;
      

  5.   

    遍历pucWriteBuf数组中的值 组成一个字符串 然后在赋值给  txt_read_webname.text
      

  6.   

    TextBox1.Text = new byte[]{.......}
      

  7.   

    不能直接把一个数组的值赋给一个非数组类的变量:txt_read_webname.text=pucWriteBuf ; 
    pucWriteBuf 是一个byte数组
    txt_read_webname.text 相当于字符串变量