Advertisement
g-stoyanov

Task05CatchTheBits

Apr 14th, 2014
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.16 KB | None | 0 0
  1. namespace Task05CatchTheBits
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Text;
  6.  
  7.     class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             int n = int.Parse(Console.ReadLine());
  12.             int step = int.Parse(Console.ReadLine());
  13.             StringBuilder bits = new StringBuilder();
  14.             for (int i = 0; i < n; i++)
  15.             {
  16.                 byte number = byte.Parse(Console.ReadLine());
  17.                 string bitNum = Convert.ToString(number, 2).PadLeft(8, '0');
  18.                 bits.Append(bitNum);
  19.             }
  20.  
  21.             int nextStep = 1;
  22.             int count = 1;
  23.             StringBuilder result = new StringBuilder();
  24.             while (nextStep < bits.Length)
  25.             {
  26.                 result.Append(bits[nextStep]);
  27.                 nextStep = 1 + (count * step);
  28.                 count++;
  29.             }
  30.  
  31.             int index = 0;
  32.             List<byte> resultBits = new List<byte>();
  33.             bool done = false;
  34.             while (!done)
  35.             {
  36.                 StringBuilder currNumBits = new StringBuilder();
  37.                 for (int i = 0; i < 8; i++)
  38.                 {
  39.                     try
  40.                     {
  41.                         currNumBits.Append(result[index + i]);
  42.                     }
  43.                     catch (Exception)
  44.                     {
  45.                         if (currNumBits.Length < 8)
  46.                         {
  47.                             if (currNumBits.Length != 0)
  48.                             {
  49.                                 currNumBits.Append(new string('0', 8 - currNumBits.Length));
  50.                             }                  
  51.                         }
  52.  
  53.                         done = true;
  54.                         break;
  55.                     }
  56.                 }
  57.                 if (currNumBits.Length != 0)
  58.                 {
  59.                     resultBits.Add(Convert.ToByte(currNumBits.ToString(), 2));
  60.                 }
  61.  
  62.                 index += 8;
  63.             }
  64.  
  65.             for (int i = 0; i < resultBits.Count; i++)
  66.             {
  67.                 Console.WriteLine(resultBits[i]);
  68.             }
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement