This documentation is archived and is not being maintained.

Convert.ToSingle Method (Byte)

.NET Framework 1.1

Converts the value of the specified 8-bit unsigned integer to the equivalent single-precision floating point number.

[Visual Basic]
Overloads Public Shared Function ToSingle( _
   ByVal value As Byte _
) As Single
[C#]
public static float ToSingle(
 byte value
);
[C++]
public: static float ToSingle(
 unsigned char value
);
[JScript]
public static function ToSingle(
   value : Byte
) : float;

Parameters

value
An 8-bit unsigned integer.

Return Value

The single-precision floating point number equivalent to the value of value.

Remarks

The following code sample illustrates the conversion of a Byte to Single, using ToSingle.

[Visual Basic] 
Public Sub ConvertByteSingle(ByVal byteVal As Byte)
    Dim singleVal As Single

    ' Byte to float conversion will not overflow.
    singleVal = System.Convert.ToSingle(byteVal)
    System.Console.WriteLine("The byte as a single is {0}.", _
                              singleVal)

    ' Single to byte conversion can overflow.
    Try
        byteVal = System.Convert.ToByte(singleVal)
        System.Console.WriteLine("The single as a byte is {0}.", _
                                  byteVal)
    Catch exception As System.OverflowException
        System.Console.WriteLine( _
            "Overflow in single-to-byte conversion.")
    End Try
End Sub

[C#] 
public void ConvertByteSingle(byte byteVal) {
    float floatVal;

    // Byte to float conversion will not overflow.
    floatVal = System.Convert.ToSingle(byteVal);
    System.Console.WriteLine("The byte as a float is {0}.",
        floatVal);

    // Float to byte conversion can overflow.
    try {
        byteVal = System.Convert.ToByte(floatVal);
        System.Console.WriteLine("The float as a byte is {0}.",
            byteVal);
    }
    catch (System.OverflowException) {
        System.Console.WriteLine(
            "The float value is too large for a byte.");
    }
}

[C++] 

   void ConvertByteSingle(Byte byteVal)
   {
      float floatVal;

      // Byte to float conversion will not overflow.
      floatVal = System::Convert::ToSingle(byteVal);
      System::Console::WriteLine(S"The Byte as a float is {0}.",
         __box(floatVal));

      // Float to Byte conversion can overflow.
      try {
         byteVal = System::Convert::ToByte(floatVal);
         System::Console::WriteLine(S"The float as a Byte is {0}.",
            __box(byteVal));
      } catch (System::OverflowException*) {
         System::Console::WriteLine(S"The float value is too large for a Byte.");
      }
   }

Requirements

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, Common Language Infrastructure (CLI) Standard

See Also

Convert Class | Convert Members | System Namespace | Convert.ToSingle Overload List

Show: