Skip to content

Instantly share code, notes, and snippets.

@tmori3y2
Last active November 27, 2016 10:11
Show Gist options
  • Save tmori3y2/debc8fd58dc8cb3f6041ea1411a243b6 to your computer and use it in GitHub Desktop.
Save tmori3y2/debc8fd58dc8cb3f6041ea1411a243b6 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
public static class Program
{
public static void Main()
{
var text = "Hello World";
text.Dump(1);
((byte)255).ToBinaryStringDump(2);
//((sbyte)-1).ToBinaryStringDump(3);
((ushort)65535).ToBinaryStringDump(4);
//((short)-1).ToBinaryStringDump(5);
((uint)4294967295).ToBinaryStringDump(6);
//((int)-1).ToBinaryStringDump(7);
((ulong)18446744073709551615).ToBinaryStringDump(8);
//((long)-1).ToBinaryStringDump(9);
(1.0f).ToBinaryStringDump(10);
(1.0).ToBinaryStringDump(11);
(1.000m).ToBinaryStringDump(12);
Decimal.GetBits(1.000m).Select(x => x.ToBinaryString()).DumpArray(13, typeof(decimal));
}
public static void Dump<T>(this T value, int line)
{
Console.WriteLine("{0, 5}: {1}", line, value.ToString());
}
public static void Dump<T>(this T value)
{
Console.WriteLine(value.ToString());
}
public static void DumpArray<T>(this IEnumerable<T> value, int line)
{
Console.Write("{0, 5}: IEnumerable<{1}>\n +", line, typeof(T).Name);
Console.WriteLine(string.Join("\n +", value.Select(x => x.ToString())));
}
public static void DumpArray<T>(this IEnumerable<T> value)
{
Console.Write("IEnumerable<{0}>\n +", typeof(T).Name);
Console.WriteLine(string.Join("\n +", value.Select(x => x.ToString())));
}
public static void DumpArray(this IEnumerable<string> value, int line, Type type)
{
Console.Write("{0, 5}: IEnumerable<{1}>\n +", line, type.Name);
Console.WriteLine(string.Join("\n +", value.Select(x => x.ToString())));
}
public static void DumpArray(this IEnumerable<string> value, Type type)
{
Console.Write("IEnumerable<{0}>\n +", type.Name);
Console.WriteLine(string.Join("\n +", value.Select(x => x.ToString())));
}
}
public static class BinaryStringHelper
{
public static void ToBinaryStringDump(this byte value, int line)
{
Console.WriteLine("{0, 5}: {1} = {2}", line, value.ToBinaryString(), value.ToString());
}
public static void ToBinaryStringDump(this sbyte value, int line)
{
Console.WriteLine("{0, 5}: {1} = {2}", line, value.ToBinaryString(), value.ToString());
}
public static void ToBinaryStringDump(this ushort value, int line)
{
Console.WriteLine("{0, 5}: {1} = {2}", line, value.ToBinaryString(), value.ToString("N0"));
}
public static void ToBinaryStringDump(this short value, int line)
{
Console.WriteLine("{0, 5}: {1} = {2}", line, value.ToBinaryString(), value.ToString("N0"));
}
public static void ToBinaryStringDump(this uint value, int line)
{
Console.WriteLine("{0, 5}: {1} = {2}", line, value.ToBinaryString(), value.ToString("N0"));
}
public static void ToBinaryStringDump(this int value, int line)
{
Console.WriteLine("{0, 5}: {1} = {2}", line, value.ToBinaryString(), value.ToString("N0"));
}
public static void ToBinaryStringDump(this ulong value, int line)
{
Console.WriteLine("{0, 5}: {1} = {2}", line, value.ToBinaryString(), value.ToString("N0"));
}
public static void ToBinaryStringDump(this long value, int line)
{
Console.WriteLine("{0, 5}: {1} = {2}", line, value.ToBinaryString(), value.ToString("N0"));
}
public static void ToBinaryStringDump(this float value, int line)
{
Console.WriteLine("{0, 5}: {1} = {2}", line, value.ToBinaryString().SetSeparator("-", new int[] { 23, 31 }), value.ToString("E8"));
}
public static void ToBinaryStringDump(this double value, int line)
{
Console.WriteLine("{0, 5}: {1} = {2}", line, value.ToBinaryString().SetSeparator("-", new int[] { 52, 63 }), value.ToString("E16"));
}
public static void ToBinaryStringDump(this decimal value, int line)
{
Console.WriteLine("{0, 5}: {1} = {2}", line, value.ToBinaryString().SetSeparator("-", new int[] { 32, 64, 96, 112, 120, 127 }), value.ToString());
}
public static string ToBinaryString(this byte value)
{
return Convert.ToString(value, 2).SetPadding(8);
}
public static string ToBinaryString(this byte value, int digits)
{
return Convert.ToString(value, 2).SetPadding(digits);
}
public static string ToBinaryString(this sbyte value)
{
return unchecked(((sbyte)value).ToBinaryString(8));
}
public static string ToBinaryString(this sbyte value, int digits)
{
return unchecked(((sbyte)value).ToBinaryString(digits));
}
public static string ToBinaryString(this ushort value)
{
return unchecked(((short)value).ToBinaryString(16));
}
public static string ToBinaryString(this ushort value, int digits)
{
return unchecked(((short)value).ToBinaryString(digits));
}
public static string ToBinaryString(this short value)
{
return Convert.ToString(value, 2).SetPadding(16);
}
public static string ToBinaryString(this short value, int digits)
{
return Convert.ToString(value, 2).SetPadding(digits);
}
public static string ToBinaryString(this uint value)
{
return unchecked(((int)value).ToBinaryString(32));
}
public static string ToBinaryString(this uint value, int digits)
{
return unchecked(((int)value).ToBinaryString(digits));
}
public static string ToBinaryString(this int value)
{
return Convert.ToString(value, 2).SetPadding(32);
}
public static string ToBinaryString(this int value, int digits)
{
return Convert.ToString(value, 2).SetPadding(digits);
}
public static string ToBinaryString(this ulong value)
{
return unchecked(((long)value).ToBinaryString(64));
}
public static string ToBinaryString(this ulong value, int digits)
{
return unchecked(((long)value).ToBinaryString(digits));
}
public static string ToBinaryString(this long value)
{
return Convert.ToString(value, 2).SetPadding(64);
}
public static string ToBinaryString(this long value, int digits)
{
return Convert.ToString(value, 2).SetPadding(digits);
}
public static string ToBinaryString(this float value)
{
var helper = new FloatHelper() { Value = value };
return helper.ToBinaryString();
}
public static string ToBinaryString(this double value)
{
var helper = new DoubleHelper() { Value = value };
return helper.ToBinaryString();
}
public static string ToBinaryString(this decimal value)
{
int[] bits = Decimal.GetBits(value);
string result = string.Empty;
for (int i = 3; i >= 0; i--)
{
result += bits[i].ToBinaryString();
}
return result;
}
public static string SetPadding(this string binaryString, int digits)
{
// adds lost digits.
int length = binaryString.Length;
//if (digits > length) return binaryString.Insert(0, new string('0', digits - length));
if (digits > 0) return binaryString.PadLeft(digits, '0');
return binaryString;
}
public static string SetSeparator(this string binaryString, string separator, int[] digitsList)
{
// adds lost digits.
var result = binaryString;
int length = result.Length;
if (digitsList.Length > 0)
{
// digitsList: from lsb to msb
foreach (var digits in digitsList)
{
var position = length - digits;
if (position > 0) result = result.Insert(position, separator);
}
}
return result;
}
}
[StructLayout(LayoutKind.Explicit)]
public struct FloatHelper
{
[FieldOffset(0)]
private int Int32Value;
[FieldOffset(0)]
public float Value;
public string ToBinaryString()
{
return Int32Value.ToBinaryString();
}
public byte[] GetBytes()
{
return BitConverter.GetBytes(Value);
}
}
[StructLayout(LayoutKind.Explicit)]
public struct DoubleHelper
{
[FieldOffset(0)]
private long Int64Value;
[FieldOffset(0)]
public double Value;
public string ToBinaryString()
{
return Int64Value.ToBinaryString();
}
public byte[] GetBytes()
{
return BitConverter.GetBytes(Value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment