C# BitConverterExt 对BitConverter的GetBytes 方法扩展

扩展类

C# BitConverterExt 扩宽方法:对BitConverter的GetBytes添加部分不用new byte[]的获取并保存到byte[] result的方法
为何要写这个类,因为C#原来的BitConverter所有的GetBytes()内部处理都是new byte[]的(ILSpy,或是Reflector都可以看到)

using System;

namespace Common
{
    /// 
    /// Extension ofr BitConverter
    /// author  :   Jave.Lin
    /// date    :   2018-03-24
    /// 
    public static class BitConverterExt
    {
        public unsafe static void GetBytes(byte[] result, byte value, int startIndex)
        {
            fixed (byte* ptr = &result[startIndex])
            {
                *ptr = value;
            }
        }

        public unsafe static void GetBytes(byte[] result, byte[] value, int resultStartIdx, int valueIdx, int valueLen)
        {
            Buffer.BlockCopy(value, valueIdx, result, resultStartIdx, valueLen);
        }

        public unsafe static void GetBytes(byte[] result, sbyte value, int startIndex)
        {
            fixed (byte* ptr = &result[startIndex])
            {
                *ptr = (byte)value;
            }
        }

        public unsafe static void GetBytes(byte[] result, sbyte[] value, int resultStartIdx, int valueIdx, int valueLen)
        {
            Buffer.BlockCopy(value, valueIdx, result, resultStartIdx, valueLen);
        }

        public unsafe static void GetBytes(byte[] result, bool value, int startIndex)
        {
            fixed (byte* ptr = &result[startIndex])
            {
                *ptr = (byte)(value == true ? 1 : 0);
            }
        }

        public unsafe static void GetBytes(byte[] result, bool[] value, int startIndex)
        {
            fixed (byte* ptrBytes = &result[startIndex])
            {
                fixed(bool* ptrValue = &value[0])
                {
                    for (int i = 0, len = value.Length; i < len; ++i)
                    {
                        *(ptrBytes + i) = (byte)(*(ptrValue + i) == true ? 1 : 0);
                    }
                }
            }
        }

        public unsafe static void GetBytes(byte[] result, char value, int startIndex)
        {
            fixed (byte* ptr = &result[startIndex])
            {
                *(char*) ptr = value;
            }
        }

        public unsafe static void GetBytes(byte[] result, char[] value, int startIndex)
        {
            fixed (byte* ptrBytes = &result[startIndex])
            {
                fixed (char* ptrValue = &value[0])
                {
                    for (int i = 0, len = value.Length; i < len; ++i)
                    {
                        *((char*)(ptrBytes) + i) = *(ptrValue + i);
                    }
                }
            }
        }

        public unsafe static void GetBytes(byte[] result, short value, int startIndex)
        {
            fixed (byte* ptr = &result[startIndex])
            {
                *(short*)ptr = value;
            }
        }

        public unsafe static void GetBytes(byte[] result, short[] value, int startIndex)
        {
            fixed (byte* ptrBytes = &result[startIndex])
            {
                fixed (short* ptrValue = &value[0])
                {
                    for (int i = 0, len = value.Length; i < len; ++i)
                    {
                        *((short*)(ptrBytes) + i) = *(ptrValue + i);
                    }
                }
            }
        }

        public unsafe static void GetBytes(byte[] result, ushort value, int startIndex)
        {
            fixed (byte* ptr = &result[startIndex])
            {
                *(ushort*)ptr = value;
            }
        }

        public unsafe static void GetBytes(byte[] result, ushort[] value, int startIndex)
        {
            fixed (byte* ptrBytes = &result[startIndex])
            {
                fixed (ushort* ptrValue = &value[0])
                {
                    for (int i = 0, len = value.Length; i < len; ++i)
                    {
                        *((ushort*)(ptrBytes) + i) = *(ptrValue + i);
                    }
                }
            }
        }

        public unsafe static void GetBytes(byte[] result, int value, int startIndex)
        {
            fixed (byte* ptr = &result[startIndex])
            {
                *(int*)ptr = value;
            }
        }

        public unsafe static void GetBytes(byte[] result, int[] value, int startIndex)
        {
            fixed (byte* ptrBytes = &result[startIndex])
            {
                fixed (int* ptrValue = &value[0])
                {
                    for (int i = 0, len = value.Length; i < len; ++i)
                    {
                        *((int*)(ptrBytes) + i) = *(ptrValue + i);
                    }
                }
            }
        }

        public unsafe static void GetBytes(byte[] result, uint value, int startIndex)
        {
            fixed (byte* ptr = &result[startIndex])
            {
                *(uint*)ptr = value;
            }
        }

        public unsafe static void GetBytes(byte[] result, uint[] value, int startIndex)
        {
            fixed (byte* ptrBytes = &result[startIndex])
            {
                fixed (uint* ptrValue = &value[0])
                {
                    for (int i = 0, len = value.Length; i < len; ++i)
                    {
                        *((uint*)(ptrBytes) + i) = *(ptrValue + i);
                    }
                }
            }
        }

        public unsafe static void GetBytes(byte[] result, long value, int startIndex)
        {
            fixed (byte* ptr = &result[startIndex])
            {
                *(long*)ptr = value;
            }
        }

        public unsafe static void GetBytes(byte[] result, long[] value, int startIndex)
        {
            fixed (byte* ptrBytes = &result[startIndex])
            {
                fixed (long* ptrValue = &value[0])
                {
                    for (int i = 0, len = value.Length; i < len; ++i)
                    {
                        *((long*)(ptrBytes) + i) = *(ptrValue + i);
                    }
                }
            }
        }

        public unsafe static void GetBytes(byte[] result, ulong value, int startIndex)
        {
            fixed (byte* ptr = &result[startIndex])
            {
                *(ulong*)ptr = value;
            }
        }

        public unsafe static void GetBytes(byte[] result, ulong[] value, int startIndex)
        {
            fixed (byte* ptrBytes = &result[startIndex])
            {
                fixed (ulong* ptrValue = &value[0])
                {
                    for (int i = 0, len = value.Length; i < len; ++i)
                    {
                        *((ulong*)(ptrBytes) + i) = *(ptrValue + i);
                    }
                }
            }
        }

        public unsafe static void GetBytes(byte[] result, float value, int startIndex)
        {
            fixed (byte* ptr = &result[startIndex])
            {
                *(float*)ptr = value;
            }
        }

        public unsafe static void GetBytes(byte[] result, float[] value, int startIndex)
        {
            fixed (byte* ptrBytes = &result[startIndex])
            {
                fixed (float* ptrValue = &value[0])
                {
                    for (int i = 0, len = value.Length; i < len; ++i)
                    {
                        *((float*)(ptrBytes) + i) = *(ptrValue + i);
                    }
                }
            }
        }

        public unsafe static void GetBytes(byte[] result, double value, int startIndex)
        {
            fixed (byte* ptr = &result[startIndex])
            {
                *(double*)ptr = value;
            }
        }

        public unsafe static void GetBytes(byte[] result, double[] value, int startIndex)
        {
            fixed (byte* ptrBytes = &result[startIndex])
            {
                fixed (double* ptrValue = &value[0])
                {
                    for (int i = 0, len = value.Length; i < len; ++i)
                    {
                        *((double*)(ptrBytes) + i) = *(ptrValue + i);
                    }
                }
            }
        }
    }
}

应用

            var one = false;
            var two = true;
            var three = true;
            var four = false;
            var arrBool = new bool[3] { true, false, true };

            var bytes = new byte[200];

            var pos = 0;
            BitConverterExt.GetBytes(bytes, one, 0);
            BitConverterExt.GetBytes(bytes, two, pos += sizeof(bool));
            BitConverterExt.GetBytes(bytes, three, pos += sizeof(bool));
            BitConverterExt.GetBytes(bytes, four, pos += sizeof(bool));
            BitConverterExt.GetBytes(bytes, 'a', pos += sizeof(bool));
            BitConverterExt.GetBytes(bytes, arrBool, pos += sizeof(char));
            BitConverterExt.GetBytes(bytes, (ushort)0xf0ff, pos += sizeof(bool) * 3);
            BitConverterExt.GetBytes(bytes, new char[] { 'A', 'B' }, pos += sizeof(ushort));
            BitConverterExt.GetBytes(bytes, new ushort[] { 0xff, 0x8 }, pos += sizeof(char) * 2);
            BitConverterExt.GetBytes(bytes, 0xff, pos += sizeof(ushort) * 2);
            BitConverterExt.GetBytes(bytes, 0xffffffu, pos += sizeof(int));
            BitConverterExt.GetBytes(bytes, 0.999f, pos += sizeof(uint));
            BitConverterExt.GetBytes(bytes, 0.999D, pos += sizeof(float));
            BitConverterExt.GetBytes(bytes, new int[] { 1, 2, 3, 4 }, pos += sizeof(double));
            BitConverterExt.GetBytes(bytes, new uint[] { 5, 6, 7, 8 }, pos += sizeof(int) * 4);
            BitConverterExt.GetBytes(bytes, new float[] { 0.1f, 0.2f, 0.3f, 0.4f }, pos += sizeof(uint) * 4);
            BitConverterExt.GetBytes(bytes, new double[] { 0.1d, 0.2d, 0.3d, 0.4d }, pos += sizeof(float) * 4);
            BitConverterExt.GetBytes(bytes, new long[] { 0xff, 0xffff, 0xffffff, 0xffffffff }, pos += sizeof(double) * 4);
            BitConverterExt.GetBytes(bytes, new ulong[] { 0xffff, 0xffffff, 0xffffffff, 0xffffffffff }, pos += sizeof(long) * 4);

输出

        [0] 0   byte
        [1] 1   byte
        [2] 1   byte
        [3] 0   byte
        [4] 97  byte
        [5] 0   byte
        [6] 1   byte
        [7] 0   byte
        [8] 1   byte
        [9] 255 byte
        [10]    240 byte
        [11]    65  byte
        [12]    0   byte
        [13]    66  byte
        [14]    0   byte
        [15]    255 byte
        [16]    0   byte
        [17]    8   byte
        [18]    0   byte
        [19]    255 byte
        [20]    0   byte
        [21]    0   byte
        [22]    0   byte
        [23]    255 byte
        [24]    255 byte
        [25]    255 byte
        [26]    0   byte
        [27]    119 byte
        [28]    190 byte
        [29]    127 byte
        [30]    63  byte
        [31]    43  byte
        [32]    135 byte
        [33]    22  byte
        [34]    217 byte
        [35]    206 byte
        [36]    247 byte
        [37]    239 byte
        [38]    63  byte
        [39]    1   byte
        [40]    0   byte
        [41]    0   byte
        [42]    0   byte
        [43]    2   byte
        [44]    0   byte
        [45]    0   byte
        [46]    0   byte
        [47]    3   byte
        [48]    0   byte
        [49]    0   byte
        [50]    0   byte
        [51]    4   byte
        [52]    0   byte
        [53]    0   byte
        [54]    0   byte
        [55]    5   byte
        [56]    0   byte
        [57]    0   byte
        [58]    0   byte
        [59]    6   byte
        [60]    0   byte
        [61]    0   byte
        [62]    0   byte
        [63]    7   byte
        [64]    0   byte
        [65]    0   byte
        [66]    0   byte
        [67]    8   byte
        [68]    0   byte
        [69]    0   byte
        [70]    0   byte
        [71]    205 byte
        [72]    204 byte
        [73]    204 byte
        [74]    61  byte
        [75]    205 byte
        [76]    204 byte
        [77]    76  byte
        [78]    62  byte
        [79]    154 byte
        [80]    153 byte
        [81]    153 byte
        [82]    62  byte
        [83]    205 byte
        [84]    204 byte
        [85]    204 byte
        [86]    62  byte
        [87]    154 byte
        [88]    153 byte
        [89]    153 byte
        [90]    153 byte
        [91]    153 byte
        [92]    153 byte
        [93]    185 byte
        [94]    63  byte
        [95]    154 byte
        [96]    153 byte
        [97]    153 byte
        [98]    153 byte
        [99]    153 byte
        [100]   153 byte
        [101]   201 byte
        [102]   63  byte
        [103]   51  byte
        [104]   51  byte
        [105]   51  byte
        [106]   51  byte
        [107]   51  byte
        [108]   51  byte
        [109]   211 byte
        [110]   63  byte
        [111]   154 byte
        [112]   153 byte
        [113]   153 byte
        [114]   153 byte
        [115]   153 byte
        [116]   153 byte
        [117]   217 byte
        [118]   63  byte
        [119]   255 byte
        [120]   0   byte
        [121]   0   byte
        [122]   0   byte
        [123]   0   byte
        [124]   0   byte
        [125]   0   byte
        [126]   0   byte
        [127]   255 byte
        [128]   255 byte
        [129]   0   byte
        [130]   0   byte
        [131]   0   byte
        [132]   0   byte
        [133]   0   byte
        [134]   0   byte
        [135]   255 byte
        [136]   255 byte
        [137]   255 byte
        [138]   0   byte
        [139]   0   byte
        [140]   0   byte
        [141]   0   byte
        [142]   0   byte
        [143]   255 byte
        [144]   255 byte
        [145]   255 byte
        [146]   255 byte
        [147]   0   byte
        [148]   0   byte
        [149]   0   byte
        [150]   0   byte
        [151]   255 byte
        [152]   255 byte
        [153]   0   byte
        [154]   0   byte
        [155]   0   byte
        [156]   0   byte
        [157]   0   byte
        [158]   0   byte
        [159]   255 byte
        [160]   255 byte
        [161]   255 byte
        [162]   0   byte
        [163]   0   byte
        [164]   0   byte
        [165]   0   byte
        [166]   0   byte
        [167]   255 byte
        [168]   255 byte
        [169]   255 byte
        [170]   255 byte
        [171]   0   byte
        [172]   0   byte
        [173]   0   byte
        [174]   0   byte
        [175]   255 byte
        [176]   255 byte
        [177]   255 byte
        [178]   255 byte
        [179]   255 byte
        [180]   0   byte
        [181]   0   byte
        [182]   0   byte
        [183]   0   byte
        [184]   0   byte
        [185]   0   byte
        [186]   0   byte
        [187]   0   byte
        [188]   0   byte
        [189]   0   byte
        [190]   0   byte
        [191]   0   byte
        [192]   0   byte
        [193]   0   byte
        [194]   0   byte
        [195]   0   byte
        [196]   0   byte
        [197]   0   byte
        [198]   0   byte
        [199]   0   byte

你可能感兴趣的:(C#)