Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace RleLib
- {
- class Program
- {
- public const int SIZE = 100;
- static byte[] MakeArray()
- {
- byte[] bytes = new byte[SIZE];
- Random rand = new Random();
- Console.WriteLine("Basic massive");
- for (int i = 0; i < SIZE; i++)
- {
- bytes[i] = Convert.ToByte(rand.Next(0, 255));
- Console.Write(bytes[i] + " ");
- }
- return bytes;
- }
- static byte[] Pack(byte[] bytes)
- {
- List<byte> Bytes = new List<byte>();
- for (int i = 0; i < (bytes.Length-1); i++)
- {
- if (bytes[i] == bytes[i + 1])
- {
- int Vol = 1;
- Vol++;
- Bytes.Add(Convert.ToByte(Vol));
- Bytes.Add(bytes[i + 1]);
- i++;
- }
- else
- {
- int Vol = 1;
- Bytes.Add(Convert.ToByte(Vol));
- Bytes.Add(bytes[i]);
- }
- }
- Bytes.Add(1);
- Bytes.Add(bytes[bytes.Length-1]);
- byte[] BYTES;
- BYTES = Bytes.ToArray();
- Console.WriteLine("\n"+"After packing");
- for (int i = 0; i < BYTES.Length; i++)
- { Console.Write(BYTES[i] + " "); }
- return BYTES;
- }
- static byte[] Unpack(byte[] BYTES)
- {
- //byte[] bytes = new byte[BYTES.Length];
- List<byte> Bytes = new List<byte>();
- for (int i = 0; i < BYTES.Length; i++)
- {
- if (i % 2 == 0)
- {
- if (BYTES[i] == 1)
- {
- Bytes.Add(BYTES[i + 1]);
- }
- if (BYTES[i] == 2)
- {
- Bytes.Add(BYTES[i + 1]);
- Bytes.Add(BYTES[i + 1]);
- }
- }
- else
- {; }
- }
- byte[] bytes;
- bytes = Bytes.ToArray();
- Console.WriteLine("\n"+"After unpacking");
- for (int i = 0; i < bytes.Length; i++)
- { Console.Write(bytes[i] + " "); }
- return bytes;
- }
- static void Main(string[] args)
- {
- byte[] bytes;
- bytes = MakeArray();
- bytes=Pack(bytes);
- bytes = Unpack(bytes);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement