Is it possible to take an input, apply a script written in C#, and write the output to a new file?

102 Views Asked by At

I have a simple run length encoding algorithim written in C#

List<byte> list = new List<byte>();
for (int i = 0; i < str.Length; i++)
{
    byte b = 1;
    char c;
    for (c = str[i]; i + 1 < str.Length && str[i + 1] == c; i++)
    {
        if (b >= byte.MaxValue)
        {
            break;
        }
        b = (byte)(b + 1);
    }
    list.Add(b);
    list.Add((byte)c);
}
return list.ToArray();

That I intend to have run against a given input, and have an output generated, containing the inputs data, now encoded with the above algorithm. What are some ways I can achieve this? I'm very VERY new to C, C#, C++ and the like some I'm a bit out of my depth with this.

1

There are 1 best solutions below

2
On

1- Create a new console application in C# using Visual Studio

2- Edit program.cs, declare your script as static function which takes an input string and returns byte[] call it EncodeString

static byte[] EncodeString(string str)
{
  var list = new List<byte>();
  for (int i = 0; i < str.Length; i++)
  {
    byte b = 1;
    char c;
    for (c = str[i]; i + 1 < str.Length && str[i + 1] == c; i++)
    {
       if (b >= byte.MaxValue)
       {
         break;
       }
       b = (byte)(b + 1);
    }
    list.Add(b);
    list.Add((byte)c);
  }
  return list.ToArray();
}

static void Main(string[] args)
{
   Console.WriteLine("Enter your input string");
   var input = Console.ReadLine();
   var encodedData = EncodeString(input);
   var filePath ="Path where you need to store file";
   System.IO.File.WriteAllBytes(filePath, ecodedData);
   Console.WriteLine("Encoded string has been saved");
   Console.WriteLine("Press any key to exist");
   Console.ReadKey();
}