public static byte[] ConcatNumByte(byte[] oldByte, int num)
{
byte[] numByte = BitConverter.GetBytes(num);
//将9000 index[0-4]0x28, 0x23, 0x00, 0x00转换为所需: 0x00, 0x00, 0x23, 0x28
Array.Reverse(numByte);
byte[] tmpB = new byte[] { };
if (num < 65536)
{
byte[] b = new byte[2];
Buffer.BlockCopy(numByte, 2, b, 0, 2); //将num[2]开始复制到b[0]开始,两位
tmpB = b;
}
else
{
tmpB = numByte;
}
byte[] newByte = new byte[oldByte.Length + tmpB.Length];
Buffer.BlockCopy(oldByte, 0, newByte, 0, oldByte.Length);
Buffer.BlockCopy(tmpB, 0, newByte, oldByte.Length, tmpB.Length);
return newByte;
}
public static byte[] ConcatStrByte(byte[] oldByte, string str)
{
byte[] strByte = Encoding.UTF8.GetBytes(str);
if(strByte.Length == 0)
{
strByte = new byte[] { 0x00 };
}
byte[] b0 = new byte[] { 0x00 }; //GetBytes只有一个字节,需要在前面补位0x00
byte[] tmp = new byte[oldByte.Length + strByte.Length + 1];
Buffer.BlockCopy(oldByte, 0, tmp, 0, oldByte.Length);
Buffer.BlockCopy(b0, 0, tmp, oldByte.Length, 1);
Buffer.BlockCopy(strByte, 0, tmp, oldByte.Length + 1, strByte.Length);
return tmp;
}
09-27
1067