Convert.FromBase64String(String) Method

 

定义

命名空间:

System

Assemblies:

System.Runtime.Extensions.dll, mscorlib.dll, netstandard.dll

将指定的字符串(它将二进制数据编码为 Base64 数字)转换为等效的 8 位无符号整数数组。

C#复制

public static byte[] FromBase64String (string s);

参数

s

String

要转换的字符串。

返回

Byte[]

与 s 等效的 8 位无符号整数数组。

异常

ArgumentNullException

s 为 null

FormatException

s 的长度(忽略空格)不是 0 或 4 的倍数。

- 或 - s 的格式无效。 s 包含非 base 64 字符、两个以上的填充字符或者在填充字符中包含非空格字符。

示例

下面的示例使用ToBase64String(Byte[])方法将字节数组转换为 string 进行 Uuencode (base-64),然后调用FromBase64String(String)方法还原原始字节数组。

C#复制

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 sring.
       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 example displays the following output:
//     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

下面是更复杂的示例创建一个 32 位整数的 20 元素数组。 然后,它使用BitConverter.GetBytes(Int32)方法,将每个元素转换为字节数组,它将存储在缓冲区中的相应位置通过调用Array.Copy(Array, Int32, Array, Int32, Int32)方法。 然后,此缓冲区传递到ToBase64String(Byte[])方法创建进行 Uuencode (base-64) 字符串。 然后,它调用FromBase64String(String)方法进行解码容易被解码的字符串,并调用BitConverter.ToInt32方法,将四个字节 (32 位整数的大小) 的每个集转换为整数。 示例输出显示已成功还原原始数组。

C#复制

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 example displays the following output:
// 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 }

注解

s 是 base 64 数字、 空格字符和尾随填充字符组成。 按升序从零开始的 base-64 位为大写字符"A"到"Z"、 小写字符"a"到"z"、 数字"0"到"9"和符号"+"和"/"。

空白字符,其 Unicode 名称和十六进制码位,将选项卡 (字符表,U + 0009)、 换行符 (换行,U + 000A) 回车 (回车符,U+000D) 和空白 (空格,U + 0020)。 任意数目的空白字符可以出现在s因为所有空白字符将被都忽略。

无值的字符、"="用于尾随填充。 结束s可以包含零个、 一个,或两个填充字符。

 重要

FromBase64String方法,可以处理包含要解码的所有数据的单个字符串。 若要解码的流的 base-64 字符数据,请使用System.Security.Cryptography.FromBase64Transform类。

适用于

.NET Core

3.0 Preview 5 2.2 2.1 2.0 1.1 1.0

.NET Framework

4.8 4.7.2 4.7.1 4.7 4.6.2 4.6.1 4.6 4.5.2 4.5.1 4.5 4.0 3.5 3.0 2.0 1.1

.NET Standard

2.1 Preview 2.0 1.6 1.5 1.4 1.3 1.2 1.1 1.0

UWP

10.0

Xamarin.Android

7.1

Xamarin.iOS

10.8

Xamarin.Mac

3.0

另请参阅

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值