Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

String Encoding Program in C#

Test Case : banagalore (of type String)
Expected Output : {30,20,21,92,20,80,32,31,02}

I Have converted them to ASCII using C# , now I’m not able to convert them to that sequence, kindly suggest some ideas .

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            Console.WriteLine("Enter The String ");
            string str = Console.ReadLine();
            byte[] b = Encoding.ASCII.GetBytes(str);
            foreach (var item in b)
            {
                Console.WriteLine(item);
            }

        }

    }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

A XOR mask would be one option.

byte[] input = Encoding.ASCII.GetBytes("bangalore");
var known_result = new byte[] { 30, 20, 21, 92, 20, 80, 32, 31, 02 };
var computed_mask = new byte[input.Length];

for (var i = 0; i < input.Length; i++)
{
    computed_mask[i] = (byte)(known_result[i] ^ input[i]);
}

byte[] test = Encoding.ASCII.GetBytes("bangalore");
for (var i = 0; i < test.Length; i++)
{
    test[i] ^= computed_mask[i];
}
Console.WriteLine(string.Join(",", test));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading