BitConverter.ToInt32(字节, 0) 

解决方案 »

  1.   


    可以用,,但是,字节数组的内容,要倒过来写...原来是16进制数: 0x01020304  ,要倒过来存即数 索引 0 里存041 里存03...

    Array.Reverse(数组)
      

  2.   


    可以用,,但是,字节数组的内容,要倒过来写...原来是16进制数: 0x01020304  ,要倒过来存即数 索引 0 里存041 里存03...
    用C#的数组的倒序方法呀
    数组.Reverse().ToArray();
      

  3.   


            /// <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;
            }没测试效率,供参考。
      

  4.   

    b = mytest[3] + (mytest[2] << 8) + (mytest[1] << 16)+(mytest[0] << 24);   这种效率是最高的,用其他的就是浪费。 
      

  5.   

    给你一个 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;
    }
      

  6.   

    可以用,,但是,字节数组的内容,要倒过来写...

    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);
                }
            }
          
      

  7.   

    System.Net.IPAddress.HostToNetworkOrder(oooo);
    System.Net.IPAddress.NetworkToHostOrder(xxxx)
    BitConverter.GetBytes(oooxxx);