StreetKatya

base64

Nov 8th, 2021
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.51 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Base64__Контест_
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             StringBuilder base64 = new StringBuilder();
  15.             List<string> alfavit = new List<string> { "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9","+","/"};
  16.             string binaryNumber = null;
  17.             using(var sr = new StreamReader("base64.in"))
  18.             {
  19.                 long n = long.Parse(sr.ReadLine());
  20.                 string[] str = sr.ReadLine().Split(' ');
  21.                 for(int i = 0; i < n; i++)
  22.                 {
  23.                     for(int j = 0; j < str[i].Length; j++)
  24.                     {
  25.                         binaryNumber += System16In2(str[i][j].ToString());
  26.                     }
  27.                 }
  28.  
  29.                 long flag = 0;
  30.                 if(binaryNumber.Length % 6 == 2)
  31.                 {
  32.                     flag = 2;
  33.                 }
  34.                 else if(binaryNumber.Length % 6 == 4)
  35.                 {
  36.                     flag = 1;
  37.                 }
  38.                 if(flag == 0)
  39.                 {
  40.                     long m = binaryNumber.Length / 6;
  41.                     for (int i = 0; i < m; i++)
  42.                     {
  43.                         string temp = binaryNumber.Substring(i * 6, 6);
  44.                         double index = System2In10(temp);
  45.                         base64.Append(alfavit[(int)index]);
  46.                     }
  47.                 }
  48.                 else if(flag == 2)
  49.                 {
  50.                     long m = binaryNumber.Length / 6;
  51.                     for (int i = 0; i < m; i++)
  52.                     {
  53.                         string temp = binaryNumber.Substring(i * 6, 6);
  54.                         double index = System2In10(temp);
  55.                         base64.Append(alfavit[(int)index]);
  56.                     }
  57.                     string lastString = binaryNumber.Substring(binaryNumber.Length - 2, 2);
  58.                     lastString += "0000";
  59.                     base64.Append(alfavit[(int)System2In10(lastString)]);
  60.                     base64.Append("==");
  61.                 }
  62.                 else
  63.                 {
  64.                     int m = binaryNumber.Length / 6;
  65.                     for (int i = 0; i < m; i++)
  66.                     {
  67.                         string temp = binaryNumber.Substring(i * 6, 6);
  68.                         double index = System2In10(temp);
  69.                         base64.Append(alfavit[(int)index]);
  70.                     }
  71.                     string lastString = binaryNumber.Substring(binaryNumber.Length - 4, 4);
  72.                     lastString += "00";
  73.                     base64.Append(alfavit[(int)System2In10(lastString)]);
  74.                     base64.Append("=");
  75.                 }
  76.             }
  77.             using (var sw = new StreamWriter("base64.out"))
  78.             {
  79.                 sw.Write(base64);
  80.             }
  81.         }
  82.         public static double System2In10(string str6)
  83.         {
  84.             double result = 0;
  85.             for (int i = 0; i < 6; i++)
  86.             {
  87.                 int temp = str6[i] % 48;
  88.                 result += temp * Math.Pow(2, 5 - i);
  89.             }
  90.             return result;
  91.         }
  92.         public static string System16In2(string str)
  93.         {
  94.             string result = "";
  95.             switch (str)
  96.             {
  97.                 case "0": result += "0000"; break;
  98.                 case "1": result += "0001"; break;
  99.                 case "2": result += "0010"; break;
  100.                 case "3": result += "0011"; break;
  101.                 case "4": result += "0100"; break;
  102.                 case "5": result += "0101"; break;
  103.                 case "6": result += "0110"; break;
  104.                 case "7": result += "0111"; break;
  105.                 case "8": result += "1000"; break;
  106.                 case "9": result += "1001"; break;
  107.                 case "A": result += "1010"; break;
  108.                 case "B": result += "1011"; break;
  109.                 case "C": result += "1100"; break;
  110.                 case "D": result += "1101"; break;
  111.                 case "E": result += "1110"; break;
  112.                 case "F": result += "1111"; break;
  113.             }
  114.             return result;
  115.         }
  116.     }
  117. }
  118.  
Add Comment
Please, Sign In to add comment