Advertisement
Guest User

Untitled

a guest
Dec 16th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace RleLib
  5. {
  6. class Program
  7. {
  8. public const int SIZE = 100;
  9. static byte[] MakeArray()
  10. {
  11. byte[] bytes = new byte[SIZE];
  12. Random rand = new Random();
  13. Console.WriteLine("Basic massive");
  14. for (int i = 0; i < SIZE; i++)
  15. {
  16. bytes[i] = Convert.ToByte(rand.Next(0, 255));
  17. Console.Write(bytes[i] + " ");
  18. }
  19. return bytes;
  20. }
  21. static byte[] Pack(byte[] bytes)
  22. {
  23. List<byte> Bytes = new List<byte>();
  24. for (int i = 0; i < (bytes.Length-1); i++)
  25. {
  26. if (bytes[i] == bytes[i + 1])
  27. {
  28. int Vol = 1;
  29. Vol++;
  30. Bytes.Add(Convert.ToByte(Vol));
  31. Bytes.Add(bytes[i + 1]);
  32. i++;
  33. }
  34. else
  35. {
  36. int Vol = 1;
  37. Bytes.Add(Convert.ToByte(Vol));
  38. Bytes.Add(bytes[i]);
  39. }
  40.  
  41. }
  42. Bytes.Add(1);
  43. Bytes.Add(bytes[bytes.Length-1]);
  44. byte[] BYTES;
  45. BYTES = Bytes.ToArray();
  46. Console.WriteLine("\n"+"After packing");
  47. for (int i = 0; i < BYTES.Length; i++)
  48. { Console.Write(BYTES[i] + " "); }
  49. return BYTES;
  50. }
  51.  
  52.  
  53. static byte[] Unpack(byte[] BYTES)
  54. {
  55. //byte[] bytes = new byte[BYTES.Length];
  56. List<byte> Bytes = new List<byte>();
  57. for (int i = 0; i < BYTES.Length; i++)
  58. {
  59. if (i % 2 == 0)
  60. {
  61. if (BYTES[i] == 1)
  62. {
  63. Bytes.Add(BYTES[i + 1]);
  64. }
  65. if (BYTES[i] == 2)
  66. {
  67. Bytes.Add(BYTES[i + 1]);
  68. Bytes.Add(BYTES[i + 1]);
  69. }
  70. }
  71. else
  72. {; }
  73. }
  74. byte[] bytes;
  75. bytes = Bytes.ToArray();
  76. Console.WriteLine("\n"+"After unpacking");
  77. for (int i = 0; i < bytes.Length; i++)
  78. { Console.Write(bytes[i] + " "); }
  79. return bytes;
  80. }
  81.  
  82. static void Main(string[] args)
  83. {
  84. byte[] bytes;
  85. bytes = MakeArray();
  86. bytes=Pack(bytes);
  87. bytes = Unpack(bytes);
  88. }
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement