-
Notifications
You must be signed in to change notification settings - Fork 45
/
Helper.cs
48 lines (44 loc) · 1.09 KB
/
Helper.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
static class Helper
{
public static IEnumerable<string> SplitByLength(this string str, int maxLength)
{
int index = 0;
while (true)
{
if (index + maxLength >= str.Length)
{
yield return str.Substring(index);
yield break;
}
yield return str.Substring(index, maxLength);
index += maxLength;
}
}
public static Byte[] Decode(this Byte[] bytes)
{
if (bytes.Length == 0) return bytes;
var i = bytes.Length - 1;
while (bytes[i] == 0 || bytes[i] == 144)
{
i--;
}
Byte[] copy = new Byte[i + 1];
Array.Copy(bytes, copy, i + 1);
return copy;
}
public static string Base64Encode(string plainText)
{
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
return System.Convert.ToBase64String(plainTextBytes);
}
public static string Base64Decode(string base64EncodedData)
{
var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
}
}