1.按照4个字节拆分为一行显示示例代码
byte[] result = new byte[] {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08 };
using (StreamWriter writer = new StreamWriter("output.txt"))
{
for (int i = 0; i < result.Length; i += 4) //按照四个字节拆分
{
byte[] tempArray = new byte[4];
int length = Math.Min(4, result.Length - i);
Array.Copy(result, i, tempArray, 0, length);
//参数1:源数组。参数2:源数组的起始位置。 参数3:目标数组,参数4:目标数组起始位置,参数5:放入目标数组的长度
writer.WriteLine(BitConverter.ToString(tempArray).Replace("-", " "));
}
}
MessageBox.Show("写入成功");
2.输出结果:
扩展:按照2个字节拆分为一行
byte[] result = new byte[] {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08 };
using (StreamWriter writer = new StreamWriter("output.txt"))
{
for (int i = 0; i < result.Length; i += 2) //按照2个字节拆分
{
byte[] tempArray = new byte[2];
int length = Math.Min(2, result.Length - i);
Array.Copy(result, i, tempArray, 0, length);
//参数1:源数组。参数2:源数组的起始位置。 参数3:目标数组,参数4:目标数组起始位置,参数5:放入目标数组的长度
writer.WriteLine(BitConverter.ToString(tempArray).Replace("-", " "));
}
}
MessageBox.Show("写入成功");