BitConverter.GetBytes 方法以什么顺序返回字节c语言bit数组怎么声明

4312人阅读
C#将值类型与字节数组互相转换,主要用到BitConverter类。示例代码如下:
byte[] bytes = { 25, 0, 0, 1, 0, 0, 0, 1 };
int numInt = BitConverter.ToInt32(bytes, 0);
short numShort = BitConverter.ToInt16(bytes, 0);
long numLong = BitConverter.ToInt64(bytes, 0);
Console.WriteLine("int: {0}", numInt);
Console.WriteLine("short: " + numShort);
Console.WriteLine("long: " + numLong);
bytes = BitConverter.GetBytes(numLong);
转换时需要注意字节数组的长度,该长度必须大于等于要转换的数值类型变量所占的字节数,如:要将字节数组转换为int,则该数组必须至少包含有4个字节项,要转换为long,则该数组必须至少包含8个字节项。BitConverter执行数值转换时,若转换为int型,则会从指定的起始索引开始,从数组中取4个字节出来转换为int,若转换为long型,则会从指定的起始索引开始,从数组中取8个字节出来转换为long。若数组的项数不足,则在用BitConverter.ToInt时,会引发System.ArgumentException,报&目标数组长度不足&的异常。
C#将值类型变量转换为字节数组时,只需调用BitConverter.GetBytes()方法即可。
版权声明:本文为博主原创文章,未经博主允许不得转载。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:37645次
排名:千里之外
原创:14篇
转载:19篇
评论:12条
(1)(1)(1)(2)(1)(4)(1)(1)(1)(1)(1)(2)(6)(2)(2)(6)1518人阅读
byte[]&&& bytTemp&&& =&&& System.Text.Encoding.Default.GetBytes(&String&);&&&
&& string&&& str&&& =&&& System.BitConverter.ToString(bytTemp);&&&
&& Console.WriteLine(str);&&&
&& string[]&&& strSplit&&& =&&& str.Split('-');&&&
&& byte[]&&& bytTemp2&&& =&&& new&&& byte[strSplit.Length];&&&
&& for&&& (int&&& i&&& =&&& 0;&&& i&&& &&&& strSplit.L&&& i++)&&&
&&&&&&&&&& bytTemp2[i]&&& =&&& byte.Parse(strSplit[i],&&& System.Globalization.NumberStyles.AllowHexSpecifier);&&&
&& string&&& str2&&& =&&& System.Text.Encoding.Default.GetString(bytTemp2);&&&
&& Console.WriteLine(str2);
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:35200次
排名:千里之外
转载:27篇
(1)(1)(1)(1)(1)(1)(1)(16)(4)(2)BitConverter.GetBytes 方法 (Boolean) (System)
此页面有用吗?
您对此内容的反馈非常重要。 请告诉我们您的想法。
更多反馈?
1500 个剩余字符
我们非常感谢您的反馈。
GetBytes 方法 (Boolean)
Expand the table of content
此文章由人工翻译。 将光标移到文章的句子上,以查看原文。
BitConverter.GetBytes 方法 (Boolean)
.NET Framework 4
以字节数组的形式返回指定的布尔值。
命名空间:
mscorlib(在 mscorlib.dll 中)
public static byte[] GetBytes(
bool value
value类型:一个布尔值。类型:[]长度为 1 的字节数组。 方法将
值的位模式转换为
// Example of the BitConverter.GetBytes( bool ) method.
class GetBytesBoolDemo
const string formatter = "{0,10}{1,16}";
// Convert a bool argument to a byte array and display it.
public static void GetBytesBool( bool argument )
byte[ ] byteArray = BitConverter.GetBytes( argument );
Console.WriteLine( formatter, argument,
BitConverter.ToString( byteArray ) );
public static void Main( )
Console.WriteLine(
"This example of the BitConverter.GetBytes( bool ) " +
"\nmethod generates the following output.\n" );
Console.WriteLine( formatter, "bool", "byte array" );
Console.WriteLine( formatter, "----", "----------" );
// Convert bool values and display the results.
GetBytesBool( false );
GetBytesBool( true );
This example of the BitConverter.GetBytes( bool )
method generates the following output.
byte array
----------
受以下版本支持:4、3.5、3.0、2.0、1.1、1.0受以下版本支持: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 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见。
您对此内容的反馈非常重要。请告诉我们您的想法。
更多反馈?
1500 个剩余字符
我们非常感谢您的反馈。
开发人员中心

我要回帖

更多关于 c语言bit数组 的文章

 

随机推荐