给你一个 C++ 的函数 static inline int ripGetValue(const BYTE * iprBuffer, const int iBytes) { int v = iprBuffer[iBytes - 1]; for (int i= iBytes - 1; i>=0; --i) { v = (v << 8) | iprBuffer[i]; } return v; }
可以用,,但是,字节数组的内容,要倒过来写...
BitConverter 类的 GetBytes 方法是根据 IsLittleEndian 的值的不同以不同的顺序返回字节数组的。// // System.BitConverter.cs // // Author: // Matt Kimball ([email protected]) // using System.Text; namespace System { public static class BitConverter { static readonly bool SwappedWordsInDouble = DoubleWordsAreSwapped (); public static readonly bool IsLittleEndian = AmILittleEndian ();
static unsafe bool AmILittleEndian () { // binary representations of 1.0: // big endian: 3f f0 00 00 00 00 00 00 // little endian: 00 00 00 00 00 00 f0 3f // arm fpa little endian: 00 00 f0 3f 00 00 00 00 double d = 1.0; byte *b = (byte*)&d; return (b [0] == 0); } unsafe static byte[] GetBytes (byte *ptr, int count) { byte [] ret = new byte [count];
for (int i = 0; i < count; i++) { ret [i] = ptr [i]; }
unsafe public static byte[] GetBytes (double value) { if (SwappedWordsInDouble) { byte[] data = new byte [8]; byte *p = (byte*)&value; data [0] = p [4]; data [1] = p [5]; data [2] = p [6]; data [3] = p [7]; data [4] = p [0]; data [5] = p [1]; data [6] = p [2]; data [7] = p [3]; return data; } else { return GetBytes ((byte *) &value, 8); } }
可以用,,但是,字节数组的内容,要倒过来写...原来是16进制数: 0x01020304 ,要倒过来存即数 索引 0 里存041 里存03...
Array.Reverse(数组)
可以用,,但是,字节数组的内容,要倒过来写...原来是16进制数: 0x01020304 ,要倒过来存即数 索引 0 里存041 里存03...
用C#的数组的倒序方法呀
数组.Reverse().ToArray();
/// <summary>
/// 自定义结构转换为byte[]类型
/// </summary>
/// <param name="anything">自定义结构</param>
/// <returns>byte[]数组</returns>
public static byte[] RawSerialize(object anything)
{
int rawsize = Marshal.SizeOf(anything);
IntPtr buffer = Marshal.AllocHGlobal(rawsize);
Marshal.StructureToPtr(anything, buffer, false);
byte[] rawdatas = new byte[rawsize];
Marshal.Copy(buffer, rawdatas, 0, rawsize);
Marshal.FreeHGlobal(buffer);
return rawdatas;
} /// <summary>
/// byte[]结构转换为自定义结构
/// </summary>
/// <param name="rawdatas">源byte[]数组</param>
/// <param name="anytype">自定义结构类型</param>
/// <returns>自定义结构变量</returns>
public static object RawDeserialize(byte[] rawdatas, Type anytype)
{
int rawsize = Marshal.SizeOf(anytype);
if (rawsize > rawdatas.Length)
return null;
IntPtr buffer = Marshal.AllocHGlobal(rawsize);
Marshal.Copy(rawdatas, 0, buffer, rawsize);
object retobj = Marshal.PtrToStructure(buffer, anytype);
Marshal.FreeHGlobal(buffer);
return retobj;
}没测试效率,供参考。
static inline int
ripGetValue(const BYTE * iprBuffer, const int iBytes) {
int v = iprBuffer[iBytes - 1];
for (int i= iBytes - 1; i>=0; --i) {
v = (v << 8) | iprBuffer[i];
}
return v;
}
BitConverter 类的 GetBytes 方法是根据 IsLittleEndian 的值的不同以不同的顺序返回字节数组的。//
// System.BitConverter.cs
//
// Author:
// Matt Kimball ([email protected])
//
using System.Text;
namespace System
{
public
static
class BitConverter
{
static readonly bool SwappedWordsInDouble = DoubleWordsAreSwapped ();
public static readonly bool IsLittleEndian = AmILittleEndian ();
static unsafe bool AmILittleEndian ()
{
// binary representations of 1.0:
// big endian: 3f f0 00 00 00 00 00 00
// little endian: 00 00 00 00 00 00 f0 3f
// arm fpa little endian: 00 00 f0 3f 00 00 00 00
double d = 1.0;
byte *b = (byte*)&d;
return (b [0] == 0);
}
unsafe static byte[] GetBytes (byte *ptr, int count)
{
byte [] ret = new byte [count];
for (int i = 0; i < count; i++) {
ret [i] = ptr [i];
}
return ret;
}
unsafe public static byte[] GetBytes (bool value)
{
return GetBytes ((byte *) &value, 1);
}
unsafe public static byte[] GetBytes (char value)
{
return GetBytes ((byte *) &value, 2);
}
unsafe public static byte[] GetBytes (short value)
{
return GetBytes ((byte *) &value, 2);
}
unsafe public static byte[] GetBytes (int value)
{
return GetBytes ((byte *) &value, 4);
}
unsafe public static byte[] GetBytes (long value)
{
return GetBytes ((byte *) &value, 8);
}
[CLSCompliant (false)]
unsafe public static byte[] GetBytes (ushort value)
{
return GetBytes ((byte *) &value, 2);
}
[CLSCompliant (false)]
unsafe public static byte[] GetBytes (uint value)
{
return GetBytes ((byte *) &value, 4);
}
[CLSCompliant (false)]
unsafe public static byte[] GetBytes (ulong value)
{
return GetBytes ((byte *) &value, 8);
}
unsafe public static byte[] GetBytes (float value)
{
return GetBytes ((byte *) &value, 4);
}
unsafe public static byte[] GetBytes (double value)
{
if (SwappedWordsInDouble) {
byte[] data = new byte [8];
byte *p = (byte*)&value;
data [0] = p [4];
data [1] = p [5];
data [2] = p [6];
data [3] = p [7];
data [4] = p [0];
data [5] = p [1];
data [6] = p [2];
data [7] = p [3];
return data;
} else {
return GetBytes ((byte *) &value, 8);
}
}
System.Net.IPAddress.NetworkToHostOrder(xxxx)
BitConverter.GetBytes(oooxxx);