how to convert a value type to byte[] in C#?

前端 未结 5 1878
南旧
南旧 2020-12-11 16:36

I want to do the equivalent of this:

byte[] byteArray;
enum commands : byte {one, two};
commands content = one;
byteArray = (byte*)&content;
相关标签:
5条回答
  • 2020-12-11 17:01

    For anyone who's interested in how it works without using BitConverter you can do it like this:

    // Convert double to byte[]
    public unsafe byte[] pack(double d) {
        byte[] packed = new byte[8]; // There are 8 bytes in a double
        void* ptr = &d; // Get a reference to the memory containing the double
        for (int i = 0; i < 8; i++) { // Each one of the 8 bytes needs to be added to the byte array
            packed[i] = (byte)(*(UInt64 *)ptr >> (8 * i)); // Bit shift so that each chunk of 8 bits (1 byte) is cast as a byte and added to array 
        }
        return packed;
    }
    
    // Convert byte[] to double
    public unsafe double unpackDouble(byte[] data) {
        double unpacked = 0.0; // Prepare a chunk of memory ready for the double
        void* ptr = &unpacked; // Reference the double memory
        for (int i = 0; i < data.Length; i++) {
            *(UInt64 *)ptr |= ((UInt64)data[i] << (8 * i)); // Get the bits into the right place and OR into the double
        }
        return unpacked;
    }
    

    In reality it's much easier and safer to use BitConverter but it's fun to know!

    0 讨论(0)
  • 2020-12-11 17:02

    The BitConverter class might be what you are looking for. Example:

    int input = 123;
    byte[] output = BitConverter.GetBytes(input);
    

    If your enum was known to be an Int32 derived type, you could simply cast its values first:

    BitConverter.GetBytes((int)commands.one);
    
    0 讨论(0)
  • 2020-12-11 17:03

    You can use the BitConverter.GetBytes method to do this.

    0 讨论(0)
  • 2020-12-11 17:05

    You can also just do a simple cast and pass it into the array constructor. It also similar in length to the BitConverter method.

    new[] { (byte)mode }
    
    0 讨论(0)
  • 2020-12-11 17:22

    To convert any value types (not just primitive types) to byte arrays and vice versa:

        public T FromByteArray<T>(byte[] rawValue)
        {
            GCHandle handle = GCHandle.Alloc(rawValue, GCHandleType.Pinned);
            T structure = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
            handle.Free();
            return structure;
        }
    
        public byte[] ToByteArray(object value, int maxLength)
        {
            int rawsize = Marshal.SizeOf(value);
            byte[] rawdata = new byte[rawsize];
            GCHandle handle =
                GCHandle.Alloc(rawdata,
                GCHandleType.Pinned);
            Marshal.StructureToPtr(value,
                handle.AddrOfPinnedObject(),
                false);
            handle.Free();
            if (maxLength < rawdata.Length) {
                byte[] temp = new byte[maxLength];
                Array.Copy(rawdata, temp, maxLength);
                return temp;
            } else {
                return rawdata;
            }
        }
    
    0 讨论(0)
提交回复
热议问题