using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace unionTest
{
class Program
{
[StructLayout(LayoutKind.Explicit)]
struct INT_BYTE
{
[FieldOffset(0)]
public int i;
[FieldOffset(0)]
public byte b0;
[FieldOffset(1)]
public byte b1;
[FieldOffset(2)]
public byte b2;
[FieldOffset(3)]
public byte b3;
};
static void Main(string[] args)
{
INT_BYTE x1 = new INT_BYTE();
INT_BYTE x2 = new INT_BYTE();
x1.i = 0x12345678;
x2.b0 = x1.b0;
x2.b1 = x1.b1;
x2.b2 = x1.b2;
x2.b3 = x1.b3;
Console.WriteLine(" x2 = 0x{0:x} ", x2.i);
}
}
}
'프로젝트' 카테고리의 다른 글
C# string to char, char to string ( 캐릭터, 문자열 변환 ) (퍼옴) (0) | 2017.03.19 |
---|---|
C# Text 파일 읽어서 출력하는 방식 두 가지.(퍼옴) (0) | 2017.03.19 |
c#에서 union 사용하기(3) (0) | 2017.03.02 |
c#에서 union 사용하기(2) (0) | 2017.03.01 |
CO2 가스 센서 - 모니터링 & 설정 (0) | 2017.03.01 |