您的位置:首页 > 其它

将基础数据类型与字节数组相互转换

2007-10-28 00:59 691 查看
BitConverter 类

将基础数据类型与字节数组相互转换。

命名空间:System

程序集:mscorlib(在 mscorlib.dll 中)

此类便于操作基本形式的类型。一个字节定义为一个 8 位无符号整数。

举例:返回由字节数组的元素转换来的 String。

// Example of the BitConverter.ToString( byte[ ] ) method.
using System;
class BytesToStringDemo
{
// Display a byte array with a name.
public static void WriteByteArray( byte[ ] bytes, string name )
{
const string underLine = "--------------------------------";
Console.WriteLine( name );
Console.WriteLine( underLine.Substring( 0,
Math.Min( name.Length, underLine.Length ) ) );
Console.WriteLine( BitConverter.ToString( bytes ) );
Console.WriteLine( );
}
public static void Main( )
{
byte[ ] arrayOne = {
0,   1,   2,   4,   8,  16,  32,  64, 128, 255 };
byte[ ] arrayTwo = {
32,   0,   0,  42,   0,  65,   0, 125,   0, 197,
0, 168,   3,  41,   4, 172,  32 };
byte[ ] arrayThree = {
15,   0,   0, 128,  16,  39, 240, 216, 241, 255,
127 };
byte[ ] arrayFour = {
15,   0,   0,   0,   0,  16,   0, 255,   3,   0,
0, 202, 154,  59, 255, 255, 255, 255, 127 };
Console.WriteLine( "This example of the " +
"BitConverter.ToString( byte[ ] ) \n" +
"method generates the following output.\n" );
WriteByteArray( arrayOne, "arrayOne" );
WriteByteArray( arrayTwo, "arrayTwo" );
WriteByteArray( arrayThree, "arrayThree" );
WriteByteArray( arrayFour, "arrayFour" );
}
}
/*
This example of the BitConverter.ToString( byte[ ] )
method generates the following output.
arrayOne
--------
00-01-02-04-08-10-20-40-80-FF
arrayTwo
--------
20-00-00-2A-00-41-00-7D-00-C5-00-A8-03-29-04-AC-20
arrayThree
----------
0F-00-00-80-10-27-F0-D8-F1-FF-7F
arrayFour
---------
0F-00-00-00-00-10-00-FF-03-00-00-CA-9A-3B-FF-FF-FF-FF-7F
*/

参见Webbased.cn相关文章
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: