Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在c语言中将对象转换为字节数组#_C#_Bytearray - Fatal编程技术网

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")

我想将对象值转换为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"));    // 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);