/
Base64Converter.cs
64 lines (51 loc) · 1.9 KB
/
Base64Converter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
using System.Linq;
namespace CSharpTrainingExample
{
public class Base64Converter
{
private const char ZERO_CHAR = '0';
private const int TWO = 2;
private const int FOUR = 4;
private const int SIX = 6;
private const int EIGHT = 8;
private const string UPPERCASE_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private const string LOWERCASE_LETTERS = "abcdefghijklmnopqrstuvwxyz";
private const string DIGITS = "0123456789";
private const string SPECIAL_SYMBOLS = "+/";
private const string EQUAL = "=";
private readonly string _allChars = string.Empty;
public Base64Converter()
{
_allChars = $"{UPPERCASE_LETTERS}{LOWERCASE_LETTERS}{DIGITS}{SPECIAL_SYMBOLS}";
}
public string ConvertToWithLinq(string text)
{
var groups = text
.ToCharArray();
var binaries = groups
.Select(x => Convert.ToString(x, TWO).PadLeft(EIGHT, ZERO_CHAR))
.ToList();
var allBinaries = string.Join(string.Empty, binaries);
var binaryGroups = allBinaries
.Select((x, i) => i)
.Where(i => i % SIX == 0)
.Select(i => String.Concat(allBinaries.Skip(i).Take(SIX)))
.Select(i => i.PadRight(SIX, ZERO_CHAR))
.Select(i => i.PadLeft(EIGHT, ZERO_CHAR))
.ToList();
var decimalGroups = binaryGroups
.Select(x => Convert.ToInt32(x, 2))
.ToList();
var charGroups = decimalGroups
.Select(x => _allChars[x])
.ToList();
string result = string.Join(string.Empty, charGroups);
while (result.Length % FOUR != 0)
{
result += EQUAL;
}
return result;
}
}
}