Created
July 19, 2022 07:02
-
-
Save JimmyPun610/ea25391b6d0a7a41135ea0c0ecfeaf94 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using deniszykov.BaseN; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using K4os.Text.BaseX; | |
namespace ConsoleApp2 | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Encode"); | |
Console.WriteLine("========================================"); | |
Console.WriteLine($"Plain 0 => Encoded {EncodeValue(0)}"); | |
Console.WriteLine($"Plain 64 => Encoded {EncodeValue(64)}"); | |
Console.WriteLine($"Plain 714 => Encoded {EncodeValue(714)}"); | |
Console.WriteLine($"Plain 16777215 => Encoded {EncodeValue(16777215)}"); | |
Console.WriteLine($"Plain 1073741824 => Encoded {EncodeValue(1073741823)}"); | |
Console.WriteLine("========================================"); | |
Console.WriteLine("Decode"); | |
Console.WriteLine("========================================"); | |
Console.WriteLine($"Encode '10' => {DecodeValue("A000000")}"); | |
Console.WriteLine($"Encode '64' => {DecodeValue("64")}"); | |
Console.WriteLine($"Encode 'AA' => {DecodeValue("AA")}"); | |
Console.WriteLine($"Encode '____' => {DecodeValue("____")}"); | |
var code = EncodeValue(64); | |
var value = DecodeValue(code); | |
} | |
public static string EncodeValue(long plainValue) | |
{ | |
string encodedStr = ""; | |
while (plainValue >= 1) | |
{ | |
int index = Convert.ToInt16(plainValue - (plainValue / 64) * 64); | |
encodedStr = Base64Code[index] + encodedStr; | |
plainValue = plainValue / 64; | |
} | |
return encodedStr; | |
} | |
public static long DecodeValue(string encodedStr) | |
{ | |
long plainValue = 0; | |
int power = encodedStr.Length - 1; | |
for (int i = 0; i <= power; i++) | |
{ | |
var mappedKey = encodedStr[power - i].ToString(); | |
var mappedValue = _Base64Code[mappedKey]; | |
var decimalValue = mappedValue * Convert.ToInt64(Math.Pow(64, i)); | |
plainValue += decimalValue; | |
} | |
return plainValue; | |
} | |
public static Dictionary<int, string> Base64Code = new Dictionary<int, string>() { | |
{ 0, "0" },{ 1, "1" },{ 2, "2" },{ 3, "3" },{ 4, "4" },{ 5, "5" },{ 6, "6" },{ 7, "7" },{ 8, "8" },{ 9, "9" }, | |
{ 10, "A" },{ 11, "B" },{ 12, "C" },{ 13, "D" },{ 14, "E" },{ 15, "F" },{ 16, "G" },{ 17, "H" },{ 18, "I" },{ 19, "J" },{ 20, "K" }, | |
{ 21, "L" },{ 22, "M" },{ 23, "N" },{ 24, "O" },{ 25, "P" },{ 26, "Q" },{ 27, "R" },{ 28, "S" },{ 29, "T" },{ 30, "U" },{ 31, "V" }, | |
{ 32, "W" },{ 33, "X" },{ 34, "Y" },{ 35, "Z" },{ 36, "a" },{ 37, "b" },{ 38, "c" },{ 39, "d" },{ 40, "e" },{ 41, "f" },{ 42, "g" }, | |
{ 43, "h" },{ 44, "i" },{ 45, "j" },{ 46, "k" },{ 47, "l" },{ 48, "m" },{ 49, "n" },{ 50, "o" },{ 51, "p" },{ 52, "q" },{ 53, "r" }, | |
{ 54, "s" },{ 55, "t" },{ 56, "u" },{ 57, "v" },{ 58, "w" },{ 59, "x" },{ 60, "y" },{ 61, "z" } | |
,{ 62, "-" },{ 63, "_" }, | |
}; | |
public static Dictionary<string, int> _Base64Code | |
{ | |
get | |
{ | |
return Enumerable.Range(0, Base64Code.Count()).ToDictionary(i => Base64Code[i], i => i); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment