static class Program
{
static void Main(string[] args)
{
int num = -8388608;
byte[] byte0 = intToBytes(num);//intToByte
int int0 = byteToInt(byte0);byteToInt
//byteToInt(new byte[] { 0x00, 0x12, 0x15, 0xff });
//INT拆分byte数组 或者此方法也可以用
int a = 123456789;
byte[] result = BitConverter.GetBytes(a);
//INT拆分byte数组 或者此方法也可以用
Console.WriteLine("输入为:" + num);
for (int i = 0; i < result.Length; i++)
{
Console.WriteLine("BYTE为:[" + i + "]" + byte0[i].ToString());
}
Console.WriteLine("输出为:" + int0);
//Console.WriteLine(a);
Console.WriteLine("Hello World!");
Console.ReadKey();
}
public static byte[] intToBytes(int value)
{
//int转换Byte
byte[] src = new byte[16];
src[0] = (byte)(value & 0xff);
src[1] = (byte)(value >> 8 & 0xff);
src[2] = (byte)(value >> 16 & 0xff);
src[3] = (byte)(value >> 24 & 0xff);
return src;
}
public static int byteToInt(byte[] src)
{
//int转换Byte
int value = 0;
value = (int)((src[0] & 0xff)
| ((src[1] & 0xff) << 8)
| ((src[2] & 0xff) << 16)
| ((src[3] & 0xff) << 24));
return value;
}
}
C# byte/int转换
于 2022-10-31 16:55:14 首次发布