How do I encode and decode a base64 string in C#?
How do I encode and decode a base64 string in C#?
I completed my post-graduation in 2013 in the engineering field. Engineering is the application of science and math to solve problems. Engineers figure out how things work and find practical uses for scientific discoveries. Scientists and inventors often get the credit for innovations that advance the human condition, but it is engineers who are instrumental in making those innovations available to the world. I love pet animals such as dogs, cats, etc.
Aryan Kumar
13-Jul-2023Sure, here is how to encode and decode a base64 string in C#:
Encoding a string to base64
To encode a string to base64, you can use the
Convert.ToBase64String()method. This method takes a byte array as a parameter and returns a base64 encoded string.For example, the following code encodes the string "Hello, world!" to base64:
C#
Decoding a base64 string
To decode a base64 string, you can use the
Convert.FromBase64String()method. This method takes a base64 encoded string as a parameter and returns a byte array.For example, the following code decodes the base64 string "SGVsbG8gV29ybGQh" to the string "Hello, world!":
C#
Here is a complete example of how to encode and decode a base64 string in C#:
C#