Skip to content

Instantly share code, notes, and snippets.

@TryJSIL
Created February 28, 2013 07:51
Show Gist options
  • Select an option

  • Save TryJSIL/5055026 to your computer and use it in GitHub Desktop.

Select an option

Save TryJSIL/5055026 to your computer and use it in GitHub Desktop.

Revisions

  1. TryJSIL created this gist Feb 28, 2013.
    36 changes: 36 additions & 0 deletions tryjsil.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    using System;
    using System.Runtime.InteropServices;

    public static class Program {
    public static unsafe void Main (string[] args) {
    var bytes = new byte[8];

    fixed (byte* pBytes = bytes) {
    var pStruct = (MyStruct*)pBytes;
    *pStruct = new MyStruct {
    Int = 2,
    Float = 3.5f
    };

    for (var i = 0; i < bytes.Length; i++)
    Console.Write("{0:X2} ", bytes[i]);

    Console.WriteLine();

    bytes[0] += 1;
    bytes[1] += 2;
    bytes[6] += 1;

    Console.WriteLine(*pStruct);
    }
    }
    }

    public struct MyStruct {
    public int Int;
    public float Float;

    public override string ToString () {
    return String.Format("Int={0:0000}, Float={1:000.000}", Int, Float);
    }
    }