Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class BitHelper
- {
- public static string ToBits(this byte i) { return IntToBitString(i, 8); }
- public static string ToBits(this ushort i) { return IntToBitString(i, 16); }
- public static string ToBits(this uint i) { return IntToBitString(i, 32); }
- public static string ToBits(this ulong i) { return IntToBitString(i, 64); }
- public static string ToBits(this sbyte i) { return ToBits((byte)i); }
- public static string ToBits(this short i) { return ToBits((ushort)i); }
- public static string ToBits(this int i) { return ToBits((uint)i); }
- public static string ToBits(this long i) { return ToBits((ulong)i); }
- public static string ToBits(this float f) { return BitConverter.ToInt32(BitConverter.GetBytes(f),0).ToBits(); }
- public static string ToBits(this double d) { return BitConverter.ToInt64(BitConverter.GetBytes(d), 0).ToBits(); }
- public static string ToBits(this decimal m)
- {
- var ints = Decimal.GetBits(m);
- return ints[0].ToBits() + " " + ints[1].ToBits() + " " + ints[2].ToBits() + " " + ints[3].ToBits();
- }
- static string IntToBitString(ulong i, int length) { return SpaceBytes(Convert.ToString((long)i, 2).PadLeft(length, '0')); }
- static string SpaceBytes(string bits)
- {
- var length = bits.Length;
- while ((length -= 8) > 0) bits = bits.Insert(length, " ");
- return bits;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement