本文整理汇总了C#中System.Convert.FromBase64String方法的典型用法代码示例。如果您正苦于以下问题:C# Convert.FromBase64String方法的具体用法?C# Convert.FromBase64String怎么用?C# Convert.FromBase64String使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Convert
的用法示例。
在下文中一共展示了Convert.FromBase64String方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
// Define a byte array.
byte[] bytes = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
Console.WriteLine("The byte array: ");
Console.WriteLine(" {0}\n", BitConverter.ToString(bytes));
// Convert the array to a base 64 string.
string s = Convert.ToBase64String(bytes);
Console.WriteLine("The base 64 string:\n {0}\n", s);
// Restore the byte array.
byte[] newBytes = Convert.FromBase64String(s);
Console.WriteLine("The restored byte array: ");
Console.WriteLine(" {0}\n", BitConverter.ToString(newBytes));
}
}
输出:
The byte array: 02-04-06-08-0A-0C-0E-10-12-14 The base 64 string: AgQGCAoMDhASFA== The restored byte array: 02-04-06-08-0A-0C-0E-10-12-14
示例2: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
// Define an array of 20 elements and display it.
int[] arr = new int[20];
int value = 1;
for (int ctr = 0; ctr <= arr.GetUpperBound(0); ctr++) {
arr[ctr] = value;
value = value * 2 + 1;
}
DisplayArray(arr);
// Convert the array of integers to a byte array.
byte[] bytes = new byte[arr.Length * 4];
for (int ctr = 0; ctr < arr.Length; ctr++) {
Array.Copy(BitConverter.GetBytes(arr[ctr]), 0,
bytes, ctr * 4, 4);
}
// Encode the byte array using Base64 encoding
String base64 = Convert.ToBase64String(bytes);
Console.WriteLine("The encoded string: ");
for (int ctr = 0; ctr <= base64.Length / 50; ctr++)
Console.WriteLine(base64.Substring(ctr * 50,
ctr * 50 + 50 <= base64.Length
? 50 : base64.Length - ctr * 50));
Console.WriteLine();
// Convert the string back to a byte array.
byte[] newBytes = Convert.FromBase64String(base64);
// Convert the byte array back to an integer array.
int[] newArr = new int[newBytes.Length/4];
for (int ctr = 0; ctr < newBytes.Length / 4; ctr ++)
newArr[ctr] = BitConverter.ToInt32(newBytes, ctr * 4);
DisplayArray(newArr);
}
private static void DisplayArray(Array arr)
{
Console.WriteLine("The array:");
Console.Write("{ ");
for (int ctr = 0; ctr < arr.GetUpperBound(0); ctr++) {
Console.Write("{0}, ", arr.GetValue(ctr));
if ((ctr + 1) % 10 == 0)
Console.Write("\n ");
}
Console.WriteLine("{0} {1}", arr.GetValue(arr.GetUpperBound(0)), "}");
Console.WriteLine();
}
}
输出:
The array: { 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535, 131071, 262143, 524287, 1048575 } The encoded string: AQAAAAMAAAAHAAAADwAAAB8AAAA/AAAAfwAAAP8AAAD/AQAA/w MAAP8HAAD/DwAA/x8AAP8/AAD/fwAA//8AAP//AQD//wMA//8H The array: { 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535, 131071, 262143, 524287, 1048575 }
示例3: Main
//引入命名空间
using System;
using System.IO;
using System.Text;
class MainClass
{
public static void Main()
{
byte[] b = Encoding.Unicode.GetBytes("string");
// Return the Base64-encoded string.
string str = Convert.ToBase64String(b);
Console.WriteLine(str);
Convert.FromBase64String(str);
// Return the decoded Unicode string.
Console.WriteLine(Encoding.Unicode.GetString(b));
}
}