哪位牛人帮小弟看下,代码如下:
public void Start0()
{
byte[] b = new byte[1];
b[0] = Convert.ToByte("10101000", 2);
int i = Convert.ToInt32(b[0]);
Console.WriteLine(i);
string s = Encoding.ASCII.GetString(b);
byte[] temp = Encoding.ASCII.GetBytes(s);
int j = Convert.ToInt32(temp[0]);
Console.WriteLine(j); }
结果打印如下:
168
63
为什么两者不一样啊?

解决方案 »

  1.   

    byte[] b = new byte[1];
    b[0] = Convert.ToByte("10101000", 2); 
    int i = Convert.ToInt32(b[0]); //返回数字 168没错
    Console.WriteLine(i); 
    string s = Encoding.ASCII.GetString(b); //可是ASCII的168已经超过字符集了。得到未知字符已经出错了,显示?(问号)。
    byte[] temp = Encoding.ASCII.GetBytes(s);//未知的字符,再获取问号的代码当然就是63咯!
    int j = Convert.ToInt32(temp[0]);//所以输出63
    Console.WriteLine(j);
      

  2.   

    168不是ascii码,所以被换成问号63了
      

  3.   

    ASCII最大值是127
    虽然扩展后的ASCII是255不过Encoding.ASCII只是us-ascii,是未扩展的。
      

  4.   

    使用Encoding.GetEncoding("GB2312")...也还是出错,那如果想表示"11111111"编码该怎么做呢?
      

  5.   

    就是为了节约空间把01011001形式表示的东西转化为普通字符的东西,这样传输的时候节约空间,其实把它们转化为byte[] 传输就行了,但是由于和其他消息一块编码所以如果能把"11111111"表示出来当然更好了。