fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6.  
  7. namespace Application
  8. {
  9. class Program
  10. {
  11. [StructLayout(LayoutKind.Explicit)]
  12. public struct Union
  13. {
  14. [FieldOffset(0)]
  15. public int integer;
  16.  
  17. [FieldOffset(0)]
  18. public byte byte0;
  19.  
  20. [FieldOffset(1)]
  21. public byte byte1;
  22.  
  23. [FieldOffset(2)]
  24. public byte byte2;
  25.  
  26. [FieldOffset(3)]
  27. public byte byte3;
  28.  
  29. }
  30.  
  31. static void Main(string[] args)
  32. {
  33. Union a = new Union();
  34. a.integer = 0x12345678;
  35.  
  36. Console.WriteLine(string.Format("integer:{0}", a.integer.ToString("x")));
  37. Console.WriteLine(string.Format("Byte:{0}{1}{2}{3}", a.byte0.ToString("x"), a.byte1.ToString("x"), a.byte2.ToString("x"), a.byte3.ToString("x")));
  38. }
  39. }
  40. }
  41.  
Success #stdin #stdout 0.03s 33896KB
stdin
Standard input is empty
stdout
integer:12345678
Byte:78563412