C# 如何将 int 转换为小端字节数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2350099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert an int to a little endian byte array?
提问by drowneath
I have this function in C# to convert a little endian byte array to an integer number:
我在 C# 中有这个函数可以将小端字节数组转换为整数:
int LE2INT(byte[] data)
{
return (data[3] << 24) | (data[2] << 16) | (data[1] << 8) | data[0];
}
Now I want to convert it back to little endian.. Something like
现在我想把它转换回小端..像
byte[] INT2LE(int data)
{
// ...
}
Any idea?
任何的想法?
Thanks.
谢谢。
采纳答案by John Knoeller
Just reverse it, Note that this this code (like the other) works only on a little Endian machine.(edit - that was wrong, since this code returns LE by definition)
只需反转它,请注意,此代码(与其他代码一样)仅适用于小型 Endian 机器。(编辑 - 那是错误的,因为此代码根据定义返回 LE)
byte[] INT2LE(int data)
{
byte[] b = new byte[4];
b[0] = (byte)data;
b[1] = (byte)(((uint)data >> 8) & 0xFF);
b[2] = (byte)(((uint)data >> 16) & 0xFF);
b[3] = (byte)(((uint)data >> 24) & 0xFF);
return b;
}
回答by MSN
Just do it in reverse:
反过来做:
result[3]= (data >> 24) & 0xff;
result[2]= (data >> 16) & 0xff;
result[1]= (data >> 8) & 0xff;
result[0]= data & 0xff;
回答by Eric
Could you use the BitConverterclass? It will only work on little endian hardware I believe, but it should handle most of the heavy lifting for you.
您可以使用BitConverter类吗?我相信它只适用于小端硬件,但它应该为您处理大部分繁重的工作。
The following is a contrived example that illustrates the use of the class:
以下是一个人为的示例,说明了该类的使用:
if(BitConverter.IsLittleEndian)
{
int someInteger = 100;
byte[] bytes = BitConverter.GetBytes(someInteger);
int convertedFromBytes = BitConverter.ToInt32(bytes, 0);
}
回答by MikeP
Depending on what you're actually doing, you could rely on letting the framework handle the details of endianness for you by using IPAddress.HostToNetworkOrderand the corresponding reverse function. Then just use the BitConverter class to go to and from byte arrays.
根据您实际执行的操作,您可以依靠让框架通过使用IPAddress.HostToNetworkOrder和相应的反向函数为您处理字节顺序的细节。然后只需使用 BitConverter 类来往和从字节数组。
回答by Sean Campbell
public static string decimalToHexLittleEndian(int _iValue, int _iBytes)
{
string sBigEndian = String.Format("{0:x" + (2 * _iBytes).ToString() + "}", _iValue);
string sLittleEndian = "";
for (int i = _iBytes - 1; i >= 0; i--)
{
sLittleEndian += sBigEndian.Substring(i * 2, 2);
}
return sLittleEndian;
}
回答by Wracky
The BitConverter
class can be used for this, and of course, it can also be used on both little and big endian systems.
该BitConverter
班可用于这一点,当然,它也可以在两个小和大端系统中使用。
Of course, you'll have to keep track of the endiannessof your data. For communications for instance, this would be defined in your protocol.
当然,您必须跟踪数据的字节序。例如,对于通信,这将在您的协议中定义。
You can then use the BitConverter
class to convert a data type into a byte array and vice versa, and then use the IsLittleEndian
flag to see if you need to convert it on your system or not.
然后,您可以使用BitConverter
该类将数据类型转换为字节数组,反之亦然,然后使用该IsLittleEndian
标志查看是否需要在系统上转换它。
The IsLittleEndian
flag will tell you the endiannessof the system, so you can use it as follows:
该IsLittleEndian
标志会告诉您系统的字节序,因此您可以按如下方式使用它:
This is from the MSDN page on the BitConverter
class.
这是来自该BitConverter
课程的 MSDN 页面。
int value = 12345678; //your value
//Your value in bytes... in your system's endianness (let's say: little endian)
byte[] bytes = BitConverter.GetBytes(value);
//Then, if we need big endian for our protocol for instance,
//Just check if you need to convert it or not:
if (BitConverter.IsLittleEndian)
Array.Reverse(bytes); //reverse it so we get big endian.
You can find the full article here.
您可以在此处找到完整的文章。
Hope this helps anyone coming here :)
希望这可以帮助任何人来到这里:)
回答by Serban Lucian
BitConverter.GetBytes(1000).Reverse<byte>().ToArray();
回答by Mendi Barel
You can use this if you don't want to use new heap allocations:
如果您不想使用新的堆分配,可以使用它:
public static void Int32ToFourBytes(Int32 number, out byte b0, out byte b1, out byte b2, out byte b3)
{
b3 = (byte)number;
b2 = (byte)(((uint)number >> 8) & 0xFF);
b1 = (byte)(((uint)number >> 16) & 0xFF);
b0 = (byte)(((uint)number >> 24) & 0xFF);
}