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];
};
public INT_BYTE x1 = new INT_BYTE();
static void Main(string[] args)
{
Program pg = new Program();
byte[] x2 = new byte[4];
x2[0] = 0x44;
x2[1] = 0x33;
x2[2] = 0x22;
x2[3] = 0x11;
unsafe
{
fixed (INT_BYTE* p = &pg.x1)
{
for (int k = 0; k < 4; k++)
{
p->b[k] = x2[k];
}
}
}
Console.WriteLine(" x1.i = 0x{0:x} ", pg.x1.i);
}
}
}
'프로젝트' 카테고리의 다른 글
C# string to char, char to string ( 캐릭터, 문자열 변환 ) (퍼옴) (0) | 2017.03.19 |
---|---|
C# Text 파일 읽어서 출력하는 방식 두 가지.(퍼옴) (0) | 2017.03.19 |
c#에서 union 사용하기(2) (0) | 2017.03.01 |
C#에서 union 사용하기(1) (0) | 2017.03.01 |
CO2 가스 센서 - 모니터링 & 설정 (0) | 2017.03.01 |