C # Bitconverterext extension of GetBytes method to Bitconverter

Source: Internet
Author: User
Tags static class
Extended Class

C # Bitconverterext Widening method: A method of adding parts of Bitconverter GetBytes without acquiring and saving the new byte[to byte[] Result
Why to write this class, because C # original Bitconverter all GetBytes () internal processing is new byte[] (Ilspy, or reflector can see)

Using System;    namespace Common {///<summary>///Extension ofr bitconverter///author:Jave.Lin///Date : 2018-03-24///</summary> public static class Bitconverterext {public unsafe static void G
            Etbytes (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); unsafe static void GetBytes (byte[] result, sbyte value, int startIndex) {fixed (byt
            e* 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); unsafe static void GetBytes (byte[] result, bool value, int startIndex) {Fixed (byte
            * ptr = &result[startindex]) {*ptr = (byte) (value = = true? 1:0);
            } 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;
            } 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); unsafe static void GetBytes (byte[] result, short value, int st Artindex) {fixed (byte* ptr = &result[startindex]) {* (short*) ptr = V
            Alue;
            } unsafe static void GetBytes (byte[] result, short[] value, int startIndex) { Fixed (byte* ptrbytes = &result[startindex]) {fixed (short* Ptrvalue = &Amp;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 s  Tartindex) {fixed (byte* ptr = &result[startindex]) {* (ushort*) ptr =
            Value
            } 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;
            } 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 sta Rtindex) {fixed (byte* ptr = &result[startindex]) {* (uint*) ptr = val
            Ue
    } 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 sta Rtindex) {fixed (byte* ptr = &result[startindex]) {* (long*) ptr = val
            Ue
            } 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); unsafe static void GetBytes (byte[] result, ulong value, int st Artindex) {fixed (byte* ptr = &result[startindex]) {* (ulong*) ptr = V
            Alue;
            } 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 st Artindex) {fixed (byte* ptr = &result[startindex]) {* (float*) ptr = V
            Alue;
}        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); unsafe static void GetBytes (byte[] result, double value, int s  Tartindex) {fixed (byte* ptr = &result[startindex]) {* (double*) ptr =
            Value
            } 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); }
                }
            }
        }
    }
}
Apply
            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);
Output
        [0] 0   byte
        [1] 1   byte
        [2] 1   byte
        [3] 0   byte
        [4]  byte
        [5] 0   byte
        [6] 1   byte
        [7] 0   byte
        [8] 1   byte
        [9] 255 byte
        [+] 11    $
        0 [
        15] (0) [[]]    255 byte
        [A]    0   byte
        []    8   byte
        []    0   byte
        [[]    255 byte
        []    0   byte
        [A]    0   byte
        []    0   byte
        [[]    255 byte
        []    255 byte
        []    255 byte
        [A]    0   [
        []    119 byte
        []    190 byte [MB
        ]    127 byte
        [30]    [
        34] [135] (a) (a)    217 byte
        [+]    206 byte
        [24]    

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.