Export (0) Print
Expand All
2 out of 14 rated this helpful - Rate this topic

Byte Structure

Represents an 8-bit unsigned integer.

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

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public value class Byte : IComparable, IFormattable, IConvertible, 
	IComparable<unsigned char>, IEquatable<unsigned char>
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class Byte extends ValueType implements IComparable, IFormattable, 
	IConvertible, IComparable<byte>, IEquatable<byte>
JScript suports the use of structures, but not the declaration of new ones.

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

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

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


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

This type is safe for multithreaded operations.

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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.