System Namespace


.NET Framework Class Library
Byte Structure

Represents an 8-bit unsigned integer.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
Syntax

Visual Basic (Declaration)
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Structure Byte _
    Implements IComparable, IFormattable, IConvertible, IComparable(Of Byte),  _
    IEquatable(Of Byte)
Visual Basic (Usage)
Dim instance As Byte
C#
[SerializableAttribute]
[ComVisibleAttribute(true)]
public struct Byte : IComparable, IFormattable, 
    IConvertible, IComparable<byte>, IEquatable<byte>
Visual C++
[SerializableAttribute]
[ComVisibleAttribute(true)]
public value class Byte : IComparable, IFormattable, 
    IConvertible, IComparable<unsigned char>, IEquatable<unsigned char>
JScript
JScript supports the use of structures, but not the declaration of new ones.
Remarks

The Byte value type represents unsigned integers with values ranging from 0 to 255.

Byte provides methods to compare instances of this type, convert the value of an instance to its string representation, and convert the string representation of a number to an instance of this type.

For information about how format specification codes control the string representation of value types, see Formatting Overview.

This type implements interfaces IComparable, IComparable<(Of <(T>)>), IFormattable, and IConvertible. Use the Convert class for conversions instead of this type's explicit interface member implementation of IConvertible.

This type is thread safe; multiple threads can concurrently read from an instance of this type.

Examples

The following example demonstrates the use of Byte when converting an array of bytes into a string of hexadecimal values.

Visual Basic
Imports System
Imports Microsoft.VisualBasic

Module HexTest
    Dim hexDigits As Char() = {"0"c, "1"c, "2"c, "3"c, "4"c, _
                               "5"c, "6"c, "7"c, "8"c, "9"c, _
                               "A"c, "B"c, "C"c, "D"c, "E"c, "F"c}

    Function ToHexString(ByVal bytes() As Byte) As String
        Dim chars(bytes.Length * 2) As Char
        Dim i As Integer

        For i = 0 To bytes.Length - 1
            Dim b As Integer = bytes(i)
            chars((i * 2)) = hexDigits(b >> 4)
            chars((i * 2) + 1) = hexDigits(b And &HF)
        Next i
        Return New String(chars)
    End Function

    Sub Main()
        Dim b As Byte() = {&H0, &H12, &H34, &H56, &HAA, &H55, &HFF}
        Console.WriteLine(ToHexString(b))
    End Sub
End Module

'
'This code example produces the following results:
'
'00123456AA55FF
'
C#
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));
    }
}

/*
This code example produces the following results:

00123456AA55FF

*/
Visual C++
ref class HexTest
{
private:
   static array<Char>^hexDigits = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

public:
   static String^ ToHexString( array<Byte>^bytes )
   {
      array<Char>^chars = gcnew array<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 gcnew String( chars );
   }

};

int main()
{
   array<Byte>^b = {0x00,0x12,0x34,0x56,0xAA,0x55,0xFF};
   Console::WriteLine( HexTest::ToHexString( b ) );
}

/*
This code example produces the following results:

00123456AA55FF

*/
Thread Safety

All members of this type are thread safe. Members that appear to modify instance state actually return a new instance initialized with the new value. As with any other type, reading and writing to a shared variable that contains an instance of this type must be protected by a lock to guarantee thread safety.

Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
See Also

Reference

Tags :


Page view tracker