Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- public class Example
- {
- public static void Main()
- {
- Console.WriteLine("Please input your sentence you want to encode");
- string stringA = Console.ReadLine();
- string[] strings = { stringA };
- Encoding asciiEncoding = Encoding.ASCII;
- // Create array of adequate size.
- byte[] bytes = new byte[49];
- // Create index for current position of array.
- int index = 0;
- Console.WriteLine("Strings to encode:");
- foreach (var stringValue in strings)
- {
- Console.WriteLine(" {0}", stringValue);
- int count = asciiEncoding.GetByteCount(stringValue);
- if (count + index >= bytes.Length)
- Array.Resize(ref bytes, bytes.Length + 50);
- int written = asciiEncoding.GetBytes(stringValue, 0,
- stringValue.Length,
- bytes, index);
- index = index + written;
- }
- Console.WriteLine("\nASCII values of your sentence:");
- Console.WriteLine("{0}", ShowByteValues(bytes, index));
- Console.WriteLine();
- Console.WriteLine("What is your cipher key?");
- var cipherKey = Console.ReadLine();
- Console.WriteLine("Your cipher key is " + cipherKey);
- // Decode Unicode byte array to a string.
- string newString = asciiEncoding.GetString(bytes, 0, index);
- Console.WriteLine("Your sentence is {0}", newString);
- Console.WriteLine("The cipher key is " + cipherKey);
- Console.ReadLine();
- }
- private static string ShowByteValues(byte[] bytes, int last)
- {
- string returnString = " ";
- for (int ctr = 0; ctr <= last - 1; ctr++)
- {
- if (ctr % 20 == 0)
- returnString += "\n ";
- returnString += String.Format("{0:X2} ", bytes[ctr]);
- }
- return returnString;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement