Export (0) Print
Expand All

Convert.ToByte Method (Int64)

Converts the value of the specified 64-bit signed integer to an equivalent 8-bit unsigned integer.

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

public static byte ToByte (
	long value
)
public static byte ToByte (
	long value
)
public static function ToByte (
	value : long
) : byte

Parameters

value

A 64-bit signed integer.

Return Value

An 8-bit unsigned integer equivalent to the value of value.

Exception typeCondition

OverflowException

value is less than Byte.MinValue or greater than Byte.MaxValue.

The following code sample illustrates the use of ToByte, converting an Int64 (long) value to a Byte :

public void ConvertLongByte(long longVal) {

    byte    byteVal = 0;

    // A conversion from Long to byte can overflow.
    try {
        byteVal = System.Convert.ToByte(longVal);
        System.Console.WriteLine("{0} as a byte is {1}",
            longVal, byteVal);
    } 
    catch (System.OverflowException) {
        System.Console.WriteLine(
            "Overflow in long-to-byte conversion.");
    }
    
    // A conversion from Byte to long cannot overflow.
    longVal = System.Convert.ToInt64(byteVal);
    System.Console.WriteLine("{0} as an Int64 is {1}",
        byteVal, longVal);
}

public void ConvertLongByte(long longVal)
{
    ubyte byteVal = 0;

    // A conversion from Long to byte can overflow.
    try {        
        byteVal = System.Convert.ToByte(longVal);
        System.Console.WriteLine("{0} as a byte is {1}", 
            System.Convert.ToString(longVal),
            System.Convert.ToString(byteVal));
    }
    catch (System.OverflowException exp) {        
        System.Console.WriteLine("Overflow in long-to-byte conversion.");
    }

    // A conversion from Byte to long cannot overflow.
    longVal = System.Convert.ToInt64(byteVal);
    System.Console.WriteLine("{0} as an Int64 is {1}", 
        System.Convert.ToString(byteVal), 
        System.Convert.ToString(longVal));
} //ConvertLongByte

Windows 98, Windows 2000 SP4, Windows Millennium Edition, 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

Community Additions

ADD
Show:
© 2014 Microsoft