Export (0) Print
Expand All

Byte Structure

Represents an 8-bit unsigned integer.

For a list of all members of this type, see Byte Members.

System.Object
   System.ValueType
      System.Byte

[Visual Basic]
<Serializable>
Public Structure Byte
   Implements IComparable, IFormattable, IConvertible
[C#]
[Serializable]
public struct Byte : IComparable, IFormattable, IConvertible
[C++]
[Serializable]
public __value struct Byte : public IComparable, IFormattable,
   IConvertible

[JScript] In JScript, you can use the structures in the .NET Framework, but you cannot define your own.

Thread Safety

This type is safe for multithreaded operations.

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, 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.

Example

[Visual Basic, C#, C++] The following example demonstrates the use of Byte when converting an array of bytes into a string of hexadecimal values.

[Visual Basic] 
Class HexTest

   Private Shared 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}
   
   Public Shared Function ToHexString(bytes() As Byte) As String

      Dim hexStr As String = ""
      Dim i As Integer
      For i = 0 To bytes.Length - 1
     hexStr = hexStr + Hex(bytes(i))
      Next i
      Return hexStr 

   End Function 'ToHexString

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

   End Sub 'Main

End Class 'HexTest


[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));
    }
}


[C++] 
__gc 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);
   }
};

int main() {

   Byte b[] = {0x00, 0x12, 0x34, 0x56, 0xAA, 0x55, 0xFF};
   Console::WriteLine(HexTest::ToHexString(b));
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

Byte Members | System Namespace | SByte | String | IComparable | IFormattable | IConvertible

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft