用C# 实现Base64解码
时间: 2023-07-17 09:03:48 浏览: 88
在C#中,可以使用`Convert.FromBase64String`方法将Base64编码的字符串解码为字节数组。以下是一个示例:
```csharp
using System;
class Program
{
static void Main()
{
string base64String = "SGVsbG8gV29ybGQh"; // Base64编码的字符串
byte[] bytes = Convert.FromBase64String(base64String);
string decodedString = System.Text.Encoding.UTF8.GetString(bytes);
Console.WriteLine(decodedString);
}
}
```
在上面的示例中,我们首先定义了一个Base64编码的字符串`base64String`。然后,使用`Convert.FromBase64String`方法将其解码为字节数组`bytes`。最后,使用`System.Text.Encoding.UTF8.GetString`方法将字节数组转换为字符串`decodedString`。
在控制台中运行该程序,将输出解码后的字符串"Hello World!"。
相关问题
c# base64解码
是C#中进行Base64解码的方法:
```csharp
public string DecodeBase64(string code_type, string code)
{
string decode = "";
byte[] bytes = Convert.FromBase64String(code);
try
{
decode = Encoding.GetEncoding(code_type).GetString(bytes);
}
catch
{
decode = code;
}
return decode;
}
```
其中,`code_type`表示编码类型,`code`表示需要解码的字符串。使用该方法可以将Base64编码的字符串解码为原始字符串。
以下是一个使用示例:
```csharp
string encodedString = "SGVsbG8gV29ybGQh";
string decodedString = DecodeBase64("UTF-8", encodedString);
Console.WriteLine(decodedString); // 输出:Hello World!
```
c#base64解码
在 C# 中,可以使用以下代码进行 Base64 解码:
```csharp
string encodedString = "SGVsbG8gV29ybGQh";
byte[] data = Convert.FromBase64String(encodedString);
string decodedString = Encoding.UTF8.GetString(data);
Console.WriteLine(decodedString);
```
其中,`encodedString` 是经过 Base64 编码后的字符串,`data` 是解码后的字节数组,`decodedString` 是解码后的字符串。在这里,使用了 `Convert.FromBase64String` 方法将 Base64 编码的字符串转换为字节数组,然后使用 `Encoding.UTF8.GetString` 方法将字节数组转换为字符串。