Advertisement
NPSF3000

BitHelper V0.1

Apr 13th, 2013
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.39 KB | None | 0 0
  1. public static class BitHelper
  2. {
  3.     public static string ToBits(this byte i) { return IntToBitString(i, 8); }
  4.     public static string ToBits(this ushort i) { return IntToBitString(i, 16); }
  5.     public static string ToBits(this uint i) { return IntToBitString(i, 32); }
  6.     public static string ToBits(this ulong i) { return IntToBitString(i, 64); }
  7.  
  8.     public static string ToBits(this sbyte i) { return ToBits((byte)i); }
  9.     public static string ToBits(this short i) { return ToBits((ushort)i); }
  10.     public static string ToBits(this int i) { return ToBits((uint)i); }
  11.     public static string ToBits(this long i) { return ToBits((ulong)i); }
  12.  
  13.     public static string ToBits(this float f) { return BitConverter.ToInt32(BitConverter.GetBytes(f),0).ToBits(); }
  14.     public static string ToBits(this double d) { return BitConverter.ToInt64(BitConverter.GetBytes(d), 0).ToBits(); }
  15.     public static string ToBits(this decimal m)
  16.     {
  17.         var ints = Decimal.GetBits(m);
  18.         return ints[0].ToBits() + " " + ints[1].ToBits() + " " + ints[2].ToBits() + " " + ints[3].ToBits();
  19.     }
  20.  
  21.     static string IntToBitString(ulong i, int length) { return SpaceBytes(Convert.ToString((long)i, 2).PadLeft(length, '0')); }
  22.     static string SpaceBytes(string bits)
  23.     {
  24.         var length = bits.Length;
  25.         while ((length -= 8) > 0) bits = bits.Insert(length, " ");
  26.         return bits;
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement