System.BitConverter.ToBoolean 方法
方法描述
返回由字节数组中指定位置的一个字节转换来的布尔值。
语法定义(C# System.BitConverter.ToBoolean 方法 的用法)
public static bool ToBoolean( byte[] value, int startIndex )
参数/返回值
参数值/返回值 | 参数类型/返回类型 | 参数描述/返回描述 |
---|---|---|
value | System-Byte[] | 字节数组。 |
startIndex | System-Int32 | value 内的起始位置。 |
返回值 | System.Boolean | 如果 value 中的 startIndex 处的字节非零,则为 true;否则为 false。 |
提示和注释
System.BitConverter.ToBoolean 方法例子
下面的代码示例使用 ToBoolean 方法将 Byte 数组的元素转换为 Boolean 值。
// Example of the BitConverter.ToBoolean method. using System; class GetBytesBoolDemo { const string formatter = "{0,5}{1,16}{2,10}"; // Convert a byte array element to bool and display it. public static void BAToBool( byte[ ] bytes, int index ) { bool value = BitConverter.ToBoolean( bytes, index ); Console.WriteLine( formatter, index, BitConverter.ToString( bytes, index, 1 ), value ); } public static void Main( ) { byte[ ] byteArray = { 0, 1, 2, 4, 8, 16, 32, 64, 128, 255 }; Console.WriteLine( "This example of the BitConverter.ToBoolean( byte[ ], " + "int ) \nmethod generates the following output. It " + "converts elements \nof a byte array to bool values.\n" ); Console.WriteLine( "initial byte array" ); Console.WriteLine( "------------------" ); Console.WriteLine( BitConverter.ToString( byteArray ) + '\n' ); Console.WriteLine( formatter, "index", "array element", "bool" ); Console.WriteLine( formatter, "-----", "-------------", "----" ); // Convert byte array elements to bool values. BAToBool( byteArray, 0 ); BAToBool( byteArray, 1 ); BAToBool( byteArray, 3 ); BAToBool( byteArray, 5 ); BAToBool( byteArray, 8 ); BAToBool( byteArray, 9 ); } } /* This example of the BitConverter.ToBoolean( byte[ ], int ) method generates the following output. It converts elements of a byte array to bool values. initial byte array ------------------ 00-01-02-04-08-10-20-40-80-FF index array element bool ----- ------------- ---- 0 00 False 1 01 True 3 04 True 5 10 True 8 80 True 9 FF True */
异常
异常 | 异常描述 |
---|---|
ArgumentNullException | value 为 null。 |
ArgumentOutOfRangeException | startIndex 小于零或大于 value 减 1 的长度。 |
版本信息
.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:
适用平台
Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。