프로젝트

c#에서 union 사용하기(2)

BLDC 2017. 3. 1. 23:04

 

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace unionTest
{
    class Program
    {
        [StructLayout(LayoutKind.Explicit)]
        public unsafe struct INT_BYTE
        {
            [FieldOffset(0)]
            public int i;
            [FieldOffset(0)]
            public fixed byte b[4];
        };
       
        static void Main(string[] args)
        {
            INT_BYTE x1 = new INT_BYTE();
            INT_BYTE x2 = new INT_BYTE();

 

            x1.i = 0x12345678;

 

            unsafe
            {
                for (int k = 0; k < 4; k++)
                    x2.b[k] = x1.b[k];
            }

 

            Console.WriteLine(" x2 = 0x{0:x} ", x2.i);
        }
    }
}
------------------------------------------------------------------

프로젝트 속성 - 빌드 옵션 변경