0
点赞
收藏
分享

微信扫一扫

FieldOffset [StructLayout(LayoutKind.Explicit)]

李雨喵 2023-06-16 阅读 210


// unionTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
union Argb
{
	unsigned char color[4];
	int num;
};
int main(int argc, char* argv[])
{
	Argb unionObj;
	unionObj.num=-100;

	printf("%d,%d,%d,%d\n",unionObj.color[0],unionObj.color[1],unionObj.color[2],unionObj.color[3]);
	return 0;
}
/*
156,255,255,255
Press any key to continue
*/

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

namespace unionSimulation
{
    [StructLayout(LayoutKind.Explicit)]
    public struct Argb32
    {
        [FieldOffset(0)]
        public Byte Blue;
        [FieldOffset(1)]
        public Byte Green;
        [FieldOffset(2)]
        public Byte Red;
        [FieldOffset(3)]
        public Byte Alpha;

        [FieldOffset(0)]
        public Int32 IntVal;
    }


    class Program
    {
        static void Main(string[] args)
        {
            Argb32 unionobj;
            unionobj.Blue=0;
            unionobj.Green=0;
            unionobj.Red = 0;
            unionobj.Alpha = 0;
            unionobj.IntVal = -100;
            Console.Out.WriteLine("{0},{1},{2},{3}", unionobj.Blue, unionobj.Green, unionobj.Red, unionobj.Alpha);
        }
    }
}




举报

相关推荐

0 条评论