方法一:
public static byte[] intToHexByte(int inInt)
{
string hexString = inInt.ToString("x4");
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
{
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
}
return returnBytes;
}
ToString("x4"); //X:代表16进制 //4:代表每次的数据位数,当位数不足时自动补0
方法二:
public byte[] intToBytes(int value)
{
byte[] src = new byte[4];
src[3] = (byte)((value >> 24) & 0xFF);
src[2] = (byte)((value >> 16) & 0xFF);
src[1] = (byte)((value >> 8) & 0xFF);//高8位
src[0] = (byte)(value & 0xFF);//低位
return src;
}
方法三:
public byte[] IntToBitConverter(int num)
{
byte[] bytes = BitConverter.GetBytes(num);
return bytes;
}