在网络中传输只能通过字节数组,因此数据需要转化成字节
由于网络的黏包和分包问题,因此需要将数字转化成字节,转化字节有两种方法
1、将字符串转化成字节,此时字节数组data的长度是变化的,取决于要转换的字符串的长度
byte[] data = Encoding.UTF8.GetBytes("");
2、将数字转化成字节
int count = 0;
byte[] data = BitConverter.GetBytes(count);
此时data数组的长度是固定是32位,因为int是32位的。
相应的,将字节数组转换成32位int数字
int count1 = BitConverter.ToInt32(data,0);