Advertisement
NPSF3000

BitSerializerPOC

Aug 16th, 2012
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.50 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ConsoleApplication2
  9. {
  10.     class Program
  11.     {
  12.         //Improved to take sign into account.
  13.         //Sign is in addition to bits allocated for storage in this version.
  14.         //Stored as {sign}{bits}
  15.         //E.g.  -5, stored in 3 bits signed is:
  16.         //       1 101
  17.         //E.g.   5, stored in 3 bits [with sign turned on]
  18.         //       0 101
  19.         //E.g.   5, stored in 3 bits no sign
  20.         //         101  
  21.         //This may differ from your exiting format - e.g. you may use two's compliments.
  22.         static void Main(string[] args)
  23.         {
  24.             int bitsPerInt = 13;
  25.  
  26.             //Create your data
  27.             var rnd = new Random();
  28.             //var data = Enumerable.Range(-5, 10).ToArray();
  29.             var data = Enumerable.Range(0, 10).Select(x => rnd.Next(-(1 << bitsPerInt), 1 << bitsPerInt)).ToArray();
  30.  
  31.             var bits = new BitSerlializer();
  32.  
  33.             //Add length header
  34.             bits.AddInt(data.Length, 8, false);
  35.             foreach (var n in data)
  36.             {
  37.                 bits.AddInt(n, bitsPerInt);
  38.                 Console.WriteLine(n);
  39.             }
  40.  
  41.             //Serialize to bytes for network transfer etc.
  42.             var bytes = bits.ToBytes();
  43.  
  44.             Console.WriteLine(new string('-', 10));
  45.             foreach (var b in bytes) Console.WriteLine(Convert.ToString(b, 2).PadLeft(8, '0'));
  46.             Console.WriteLine(new string('-', 10));
  47.  
  48.             //Deserialize
  49.             bits = new BitSerlializer(bytes);
  50.             //Get Length Header
  51.             var count = bits.ReadInt(8, false);
  52.             for (int i = 0; i < count; i++)
  53.                 Console.WriteLine(bits.ReadInt(bitsPerInt));
  54.  
  55.             Console.ReadLine();
  56.         }
  57.  
  58.         public class BitSerlializer
  59.         {
  60.             List<byte> bytes;
  61.             int Position { get; set; }
  62.  
  63.             public BitSerlializer(byte[] initialData = null)
  64.             {
  65.                 if (initialData == null)
  66.                     bytes = new List<byte>();
  67.                 else
  68.                     bytes = new List<byte>(initialData);
  69.             }
  70.  
  71.             public byte[] ToBytes() { return bytes.ToArray(); }
  72.  
  73.             public void Addbit(bool val)
  74.             {
  75.                 if (Position % 8 == 0) bytes.Add(0);
  76.                 if (val) bytes[Position / 8] += (byte)(128 >> (Position % 8));
  77.                 Position++;
  78.             }
  79.  
  80.             public void AddInt(int i, int length, bool isSigned = true)
  81.             {
  82.                 if (isSigned) Addbit(i < 0);
  83.                 if (i < 0) i = -i;
  84.  
  85.                 for (int pos = --length; pos >= 0; pos--)
  86.                 {
  87.                     var val = (i & (1 << pos)) != 0;
  88.                     Addbit(val);
  89.                 }
  90.             }
  91.  
  92.             public bool ReadBit()
  93.             {
  94.                 var val = (bytes[Position / 8] & (128 >> (Position % 8))) != 0;
  95.                 ++Position;
  96.                 return val;
  97.             }
  98.  
  99.             public int ReadInt(int length, bool isSigned = true)
  100.             {
  101.                 var val = 0;
  102.                 var sign = isSigned && ReadBit() ? -1 : 1;
  103.  
  104.                 for (int pos = --length; pos >= 0; pos--)
  105.                     if (ReadBit())
  106.                         val += 1 << pos;
  107.  
  108.                 return val * sign;
  109.             }
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement