Could anybody give me comment here to help me understand this code? please, I wa
ID: 3597936 • Letter: C
Question
Could anybody give me comment here to help me understand this code? please, I want details comment. thank you in advance
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Framing
{
class Program
{
static void Main(string[] args)
{
Dictionary charEncoding = new Dictionary();
charEncoding.Add("A", "01000111");
charEncoding.Add("B", "11100011");
charEncoding.Add("C", "11000001");
charEncoding.Add("ESC", "11100000");
charEncoding.Add("FLAG", "01111110");
Console.WriteLine("Enter Sequence: ");
string input = Console.ReadLine().Trim().ToUpper();
Console.Write("Byte count: " + Convert.ToString((input.Split(' ').Length + 1), 2).PadLeft(8, '0'));
foreach (string str in input.Split(' '))
{
if (!string.IsNullOrEmpty(str))
{
Console.Write(" "+charEncoding[str]);
}
}
Console.WriteLine();
Console.WriteLine("Flag bytes with byte stuffing: " + charEncoding["FLAG"]);
foreach (string str in input.Split(' '))
{
if (!string.IsNullOrEmpty(str))
{
Console.Write(" " + charEncoding[str]);
if (str.Equals("ESC"))
{
Console.Write(" " + charEncoding[str]);
}
}
}
Console.Write(" " + charEncoding["FLAG"]);
Console.ReadLine();
}
}
}
Explanation / Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Framing{
class Program
{
static void Main(string[] args){
/*Instantiate a new Dictionary. The Dictionary Class is present in the System.Collections.Generic;*/
Dictionary charEncoding = new Dictionary();
/*The name of the Dictionary is charEncoding. The words A, B, C, ESC, FLAG are stored in the dictionary.*/
charEncoding.Add("A", "01000111");
charEncoding.Add("B", "11100011");
charEncoding.Add("C", "11000001");
charEncoding.Add("ESC", "11100000");
charEncoding.Add("FLAG", "01111110");
/*This will print "Enter Sequence: " on the screen and is asking for an input.*/
Console.WriteLine("Enter Sequence: ");
//example input = " a c b esc flag "
/*The entered input is stored in the string variable named = input.
*Trim() method removes from the current string all leading and trailing white-space characters.
*ToUpper() method converts the input to all UPPERCASE.
*/
string input = Console.ReadLine().Trim().ToUpper();
//input = "A C B ESC FLAG"
/*Split() method will split the input after each occurence of ' ' i.e. space.
*The substrings will be stored in an array. The 1 in the (input.Split(' ').Length + 1) shows the size of the array in which each of these substringsare going to be stored.*/
/*PadLeft(8, '0') will add zeroes to the string. It can be observed that the Dictionary contains encodings of length 8.
The format is str = str.PadLeft(8, '0');*/
/*Convert.ToString((input.Split(' ').Length + 1), 2) will return substrings and each of them will be padded with zeroes.*/
Console.Write("Byte count: " + Convert.ToString((input.Split(' ').Length + 1), 2).PadLeft(8, '0'));
/*This will be performed for each substring obtained previously*/
foreach (string str in input.Split(' ')){
if (!string.IsNullOrEmpty(str)){ //IsNullOrEmpty(str) returns TRUE if str is empty.
//Do this if string is NOT empty.
Console.Write(" "+charEncoding[str]);
//Print the given encoding for the str
}
}
//Print a new line
Console.WriteLine();
Console.WriteLine("Flag bytes with byte stuffing: " + charEncoding["FLAG"]);
/*This will be performed for each substring obtained by doing Split(' ')*/
foreach (string str in input.Split(' ')){
if (!string.IsNullOrEmpty(str)){
//Do this if string is NOT empty.
Console.Write(" " + charEncoding[str]);
//Print the given encoding of the str
if (str.Equals("ESC")){
Console.Write(" " + charEncoding[str]);
}
}
}
//This will print encoding for the FLAG.
Console.Write(" " + charEncoding["FLAG"]);
//This is probably written to exit when Enter is pressed.
Console.ReadLine();
}
}
}
/*The Code is clearly a bit incomplete but the definitions and the usage of the methods will remain the same as explained.*/