Avatar ·

How to encode and decode a string in Base64 in C#?

Hi everyone! I'm a beginner C# developer and I've encountered a task: I need to transfer binary data (e.g., an image) in JSON, but JSON only supports strings. I've heard that Base64 is used for this. But I can't find a simple and working example of how to do this in C#. I need to: 1. Take a regular string (e.g., "Hello, World!") and encode it to Base64. 2. Then take the encoded Base64 string and decode it back to the original string. I tried writing my own custom solution, but it doesn't work. I've seen that .NET has some classes in `System.Convert` and `System.Text`, but I don't understand how to use them correctly. Here's my unsuccessful code: ```csharp string original = "Hello, World!"; byte[] bytes = Encoding.ASCII.GetBytes(original); string encoded = Convert.ToBase64String(by