c# Byte类型数据转换成字节数组时,为什么占用两个字节?
bytex1=255;byte[]targetByte=BitConverter.GetBytes(x1);MessageBox.Show(targetByte.Leng...
byte x1 = 255;
byte[] targetByte = BitConverter.GetBytes(x1);
MessageBox.Show(targetByte.Length.ToString()); 展开
byte[] targetByte = BitConverter.GetBytes(x1);
MessageBox.Show(targetByte.Length.ToString()); 展开
展开全部
BitConverter.GetBytes
以字节数组的形式返回指定的 16 位有符号整数值。
具体参看msdn
// Example of the BitConverter.GetBytes( short ) method.
using System;
class GetBytesInt16Demo
{
const string formatter = "{0,10}{1,13}";
// Convert a short argument to a byte array and display it.
public static void GetBytesInt16( short 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( short ) " +
"\nmethod generates the following output.\n" );
Console.WriteLine( formatter, "short", "byte array" );
Console.WriteLine( formatter, "-----", "----------" );
// Convert short values and display the results.
GetBytesInt16( 0 );
GetBytesInt16( 15 );
GetBytesInt16( -15 );
GetBytesInt16( 10000 );
GetBytesInt16( -10000 );
GetBytesInt16( short.MinValue );
GetBytesInt16( short.MaxValue );
}
}
/*
This example of the BitConverter.GetBytes( short )
method generates the following output.
short byte array
----- ----------
0 00-00
15 0F-00
-15 F1-FF
10000 10-27
-10000 F0-D8
-32768 00-80
32767 FF-7F
*/
以字节数组的形式返回指定的 16 位有符号整数值。
具体参看msdn
// Example of the BitConverter.GetBytes( short ) method.
using System;
class GetBytesInt16Demo
{
const string formatter = "{0,10}{1,13}";
// Convert a short argument to a byte array and display it.
public static void GetBytesInt16( short 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( short ) " +
"\nmethod generates the following output.\n" );
Console.WriteLine( formatter, "short", "byte array" );
Console.WriteLine( formatter, "-----", "----------" );
// Convert short values and display the results.
GetBytesInt16( 0 );
GetBytesInt16( 15 );
GetBytesInt16( -15 );
GetBytesInt16( 10000 );
GetBytesInt16( -10000 );
GetBytesInt16( short.MinValue );
GetBytesInt16( short.MaxValue );
}
}
/*
This example of the BitConverter.GetBytes( short )
method generates the following output.
short byte array
----- ----------
0 00-00
15 0F-00
-15 F1-FF
10000 10-27
-10000 F0-D8
-32768 00-80
32767 FF-7F
*/
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询