C# 仿写 共用体


    [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi, Pack = 1)]
    public struct strUion
    {
        [FieldOffset(0)]
        public ushort crc16;

        [FieldOffset(0)]
        public byte crc16L;

        [FieldOffset(1)]
        public byte crc16H;

        public strUion(ushort _crc16)
        {
            crc16 = _crc16;
            crc16H = 0;
            crc16L = 0;
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            strUion su = new strUion(0);

            su.crc16 = 0x0102;

            Console.WriteLine(su.crc16H.ToString("X2") + "  " + su.crc16L.ToString("X2"));

            Console.ReadLine();
        }
    }

posted on 2023-08-06 18:10  panpeng666  阅读(14)  评论(0编辑  收藏  举报

导航