另外:简易的模仿一下插座通讯协议(注意:只是简易,真正用在插座里面,需要另外加协议号等等):,
包头只有一个int32类型用于包体的长度
包体:
,,,, (
,,,,,,,, int32:内容的长度
,,,,,,,,字符串:内容
,,,,)
,,,, int32,这里故意加的(免得成光棍内容)(在实际插座中,你可以表示年龄啥的! ! !)
代码如下:
using 系统; using System.Collections.Generic; using 来; using 包含; using System.Threading.Tasks; namespace BitBitConverterTest.ainy { ,,,///,& lt; summary> ,,,///,将内容变成规则的二进制数据,以便发送 ,,,///,& lt;/summary> ,,,public class BitConverToByte ,,,{ ,,,,,,,private readonly string 语境; ,,,,,,,private readonly Int32 标志; ,,,,,,,public BitConverToByte (, string context ,, Int32 mark ) ,,,,,,,{ ,,,,,,,,,,,this.context =,上下文; ,,,,,,,,,,,this.mark =,马克; ,,,,,,,} ,,,,,,,///,& lt; summary> ,,,,,,,///,返回二进制数组 ,,,,,,,///,& lt;/summary> ,,,,,,,///,& lt; returns>, int32长度,+,context +, mark lt;/returns> ,,,,,,,public byte [], GetContextBytes () ,,,,,,,{ ,,,,,,,,,,,//,内容的二进制 ,,,,,,,,,,,byte [], myContext =, Encoding.UTF8.GetBytes (this.context); ,,,,,,,,,,,//,标示的二进制(,测试BitConverter ) ,,,,,,,,,,,byte [], myMark =, BitConverter.GetBytes (this.mark); ,,,,,,,,,,,//,关于内容长度,myContext + myMark ,,,,,,,,,,,byte [], myBytesLenght =, BitConverter.GetBytes(时间+ myContext.Length myMark.Length); ,,,,,,,,,,,//,关于内容的长度 ,,,,,,,,,,,byte [], myContextLength =, BitConverter.GetBytes (myContext.Length); ,,,,,,,,,,,byte [], reslut =, new 字节(时间+ myContext.Length myContextLength.Length +, myMark.Length +, myBytesLenght.Length); ,,,,,,,,,,,myBytesLenght.CopyTo (reslut, 0); ,,,,,,,,,,,myContextLength.CopyTo (myBytesLenght.Length reslut也); ,,,,,,,,,,,myContext.CopyTo (reslut, myBytesLenght.Length +, myContextLength.Length); ,,,,,,,,,,,myMark.CopyTo (reslut, myBytesLenght.Length +, myContext.Length +, myContextLength.Length); ,,,,,,,,,,,return reslut; ,,,,,,,} ,,,} }
using 系统; using System.Collections.Generic; using 来; using 包含; using System.Threading.Tasks; namespace BitBitConverterTest.ainy { ,,,///,& lt; summary> ,,,///,将二进制转化为内容 ,,,///,& lt;/summary> ,,,public class BitConverToContext ,,,{ ,,,,,,,//整个二进制数据 ,,,,,,,private readonly byte[],目标; ,,,,,,,//文本二进制(String)数据 ,,,,,,,public string Context {, get ,, private ;,} ,,,,,,,//标记数据 ,,,,,,,public Int32 Mark {,得到;private ;,} null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null nullc#基础数据与字节