C# 在c语言中将对象转换为字节数组#
我想将对象值转换为c#中的字节数组 例:C# 在c语言中将对象转换为字节数组#,c#,bytearray,C#,Bytearray,我想将对象值转换为c#中的字节数组 例: 谢谢您可以查看该方法: 另外,请确保在定义第一个字节时已将其考虑在内。使用将使用系统的本机endianness将整数转换为字节[]数组 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")
谢谢您可以查看该方法: 另外,请确保在定义第一个字节时已将其考虑在内。使用将使用系统的本机endianness将整数转换为
字节[]
数组
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 i = 2200;
byte[] bytes = BitConverter.GetBytes(i);
Console.WriteLine(bytes[0].ToString("x"));
Console.WriteLine(bytes[1].ToString("x"));
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);