Last active
October 28, 2020 12:52
-
-
Save Gummiees/9bbaed4d1837912f31bdfad1815cb874 to your computer and use it in GitHub Desktop.
Encode and decode strings with C#
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 System; | |
public class Program | |
{ | |
/** | |
* You can this code at https://dotnetfiddle.net/uH4B69 | |
*/ | |
public static void Main() | |
{ | |
string text = "Hello World!"; | |
string encoded = Encode(text); | |
string decoded = Decode(encoded); | |
Console.WriteLine($"text: {text}"); | |
Console.WriteLine($"encoded: {encoded}"); | |
Console.WriteLine($"decoded: {decoded}"); | |
} | |
public static string Encode(string stringToEncode) | |
{ | |
byte[] encoded = System.Text.Encoding.Unicode.GetBytes(stringToEncode); | |
string result = Convert.ToBase64String(encoded); | |
return result; | |
} | |
public static string Decode(string stringToDecode) | |
{ | |
byte[] decoded = Convert.FromBase64String(stringToDecode); | |
string result = System.Text.Encoding.Unicode.GetString(decoded); | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment