我希望你能帮我将C#中的对象值转换为字节数组。
示例:
示例:
step 1. Input : 2200
step 2. After converting Byte : 0898
step 3. take first byte(08)
Output: 08
谢谢
byte[] bytes = BitConverter.GetBytes(2200);
Console.WriteLine(bytes[0]);
BitConverter.GetBytes
可以将您的整数转换为byte[]
数组,使用系统的本机字节顺序。short s = 2200;
byte[] b = BitConverter.GetBytes(s);
Console.WriteLine(b[0].ToString("X")); // 98 (on my current system)
Console.WriteLine(b[1].ToString("X")); // 08 (on my current system)
short s = 2200;
byte[] b = new byte[] { (byte)(s >> 8), (byte)s };
Console.WriteLine(b[0].ToString("X")); // 08 (always)
Console.WriteLine(b[1].ToString("X")); // 98 (always)
int number = 2200;
byte[] br = BitConverter.GetBytes(number);