Byte 结构
表示一个 8 位无符号整数。
命名空间:System
程序集:mscorlib(在 mscorlib.dll 中)
程序集:mscorlib(在 mscorlib.dll 中)
[SerializableAttribute] [ComVisibleAttribute(true)] public struct Byte : IComparable, IFormattable, IConvertible, IComparable<byte>, IEquatable<byte>
/** @attribute SerializableAttribute() */ /** @attribute ComVisibleAttribute(true) */ public final class Byte extends ValueType implements IComparable, IFormattable, IConvertible, IComparable<byte>, IEquatable<byte>
Byte 值类型表示值介于 0 和 255 之间的无符号整数。
Byte 为比较该类型的实例、将实例值转换为它的字符串表示形式以及将数字的字符串表示形式转换为该类型的实例提供了相应的方法。
有关格式规范代码如何控制值类型的字符串表示形式的信息,请参见 格式化概述。
此类型实现 IComparable、IComparable、IFormattable 和 IConvertible 接口。使用 Convert 类进行转换,而不是使用此类型的 IConvertible 显式接口成员实现。
这种类型是线程安全的;可以同时从该类型的实例读取多个线程。
下面的示例说明将字节数组转换为十六进制值字符串时 Byte 的用法。
class HexTest { static char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; public static string ToHexString(byte[] bytes) { char[] chars = new char[bytes.Length * 2]; for (int i = 0; i < bytes.Length; i++) { int b = bytes[i]; chars[i * 2] = hexDigits[b >> 4]; chars[i * 2 + 1] = hexDigits[b & 0xF]; } return new string(chars); } static void Main() { byte[] b = {0x00, 0x12, 0x34, 0x56, 0xAA, 0x55, 0xFF}; Console.WriteLine(ToHexString(b)); } }
class HexTest
{
private static char hexDigits[] = { '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
public static String ToHexString(ubyte bytes[])
{
char chars[] = new char[bytes.get_Length() * 2];
for (int i = 0; i < bytes.get_Length(); i++) {
int b = System.Convert.ToInt32(
System.Convert.ToString(bytes.get_Item(i)));
chars.set_Item((i * 2), hexDigits.get_Item(b >> 4));
chars.set_Item((i * 2 + 1), hexDigits.get_Item(b & 0xF));
}
return new String(chars);
} //ToHexString
public static void main(String[] args)
{
ubyte b[] = { 0x0, 0x12, 0x34, 0x56, 0xAA, 0x55, 0xFF };
Console.WriteLine(ToHexString(b));
} //main
} //HexTest
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
