Skip to content

Instantly share code, notes, and snippets.

View mgravell's full-sized avatar
🏠
Working from home

Marc Gravell mgravell

🏠
Working from home
View GitHub Profile
using Newtonsoft.Json;
using System;
using System.Text;
static class P
{
static void Main()
{
var base64 = @"e0tleSA6ICdhYmMnLCBpc0V4aXN0czogJ3RydWUnfQ==";
var blob = Convert.FromBase64String(base64);
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
Console.WriteLine(Unsafe.SizeOf<Flags>()); // 50, it worked!
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Flags
{
public ushort frameCnt; //0-1
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
Console.WriteLine(Unsafe.SizeOf<Flags>()); // 50, it worked!
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Flags
{
public ushort frameCnt; //0-1
using System;
using System.Collections.Generic;
static class P
{
static void Main()
{
var bytes = new List<byte>();
bytes.AddRange(new byte[] { 0, 1, 2, 3 });
bytes.AddRange(new byte[] { 4, 5 });
using System;
class Program
{
static void Main()
{
int z = 42;
int x = Foo(z);
Console.WriteLine($"{x}, {z}");
// get: 871, 42
// expected: 871, 46
@mgravell
mgravell / decode.cs
Last active September 15, 2020 10:39
using System;
using System.Buffers.Binary;
static class P
{
static void Main()
{
// sample taken from comtrade91.pdf section 6.6
var buffer = new byte[] {
using ProtoBuf;
using System;
using System.IO;
static class Funchelper
{
static void Main()
{
var obj = new NotAForm();
obj.DoTheThing();
readonly struct Foo
{
private readonly int[] _arr;
public Foo(int length) => _arr = new int[length];
public int Length => _arr?.Length ?? 0;
public ref int this[int index] => ref _arr[index];
public FooEnumerator GetEnumerator() => new FooEnumerator(_arr);
public struct FooEnumerator
using System;
class Foo
{
int _value;
public ref int Value => ref _value;
static void Main()
{
var obj = new Foo { Value = 12 };
Console.WriteLine(obj.Value);
string s = string.Join(",", Enumerable.Repeat("blah", 50));
var charSpan = s.AsSpan();
var vectorized = MemoryMarshal.Cast<char, Vector<ushort>>(charSpan);
Console.WriteLine($"vector chunks: " + vectorized.Length);
foreach(var v in vectorized)
{
if (Vector.EqualsAny(v, spaces)) { ...}
}
// todo: mop up any remainded