从Socket收到的数据包,转换后得到的字串无法分辩
            byte[] Bytes = new byte[1024];
            int bytes;
            bytes = Socket.Receive(Bytes, recvBytes.Length, 0);
            Str = Encoding.ASCII.GetString(Bytes, 0, bytes); 
            Console.WriteLine(Str);比如,我收到“534AC21100048A570098006666660000C9E8”这个数据包是16进制的,其中每两位是一个字节,534AC211为固定值,00048A57需转换为297559,后面0098006666660000C9E8不需要转换。按上面的方法,得到的string是?
我如何在收到这个数据包后将它转为可以读出的string呢?没查到资料,请高手指点

解决方案 »

  1.   

    byte[] Bytes = new byte[1024]; 
                int bytes= Socket.Receive(Bytes, recvBytes.Length, 0); 
                Str = Encoding.ASCII.GetString(Bytes, 18, bytes-18); 
                Console.WriteLine(Str); 
    我不知道你后面的数据是什么 如后E8后面跟的是0 就19位开始转.
      

  2.   

     str = Encoding.ASCII.GetString(bytes, 8, 6);
    从第8位0开始取6位就可以把00048A57需转换为297559,
      

  3.   

    看来是我没说清楚,收到的数据包是以下方式的
    0x53,0x4A,0xC2,0x11,0x00,0x00,0x04,0x8A,0x57,0x00,0x98,0x00,0x66,0x66,0x66,0x66,0x00,0x00,0xC9,0xE8
    所以直接软为字符串只出来"?"而不可读。
    我在问的是,这个数据包是16进制的,其中每一个字节的内容是两位数据。如何得到如0x53的这个内容53,而不是转义的字符?
    初学,不会啊
      

  4.   


    if(Bytes[0]!=0x53 || Bytes[1]!=0x4A)
    {
      //头标记错误
    }
    //Convert 0x00048A57 to int
    int data;
    data=Bytes[5];
    data=data*256+ Bytes[6];
    data=data*256+ Bytes[7];
    data=data*256+ Bytes[8];