다음을 통해 공유


BitConverter.ToSingle 메서드

정의

오버로드

ToSingle(ReadOnlySpan<Byte>)

읽기 전용 바이트 범위를 단정밀도 부동 소수점 값으로 변환합니다.

ToSingle(Byte[], Int32)

4바이트에서 변환된 단정밀도 부동 소수점 숫자를 바이트 배열의 지정된 위치에 반환합니다.

ToSingle(ReadOnlySpan<Byte>)

읽기 전용 바이트 범위를 단정밀도 부동 소수점 값으로 변환합니다.

public:
 static float ToSingle(ReadOnlySpan<System::Byte> value);
public static float ToSingle (ReadOnlySpan<byte> value);
static member ToSingle : ReadOnlySpan<byte> -> single
Public Shared Function ToSingle (value As ReadOnlySpan(Of Byte)) As Single

매개 변수

value
ReadOnlySpan<Byte>

변환할 바이트를 포함하는 읽기 전용 범위입니다.

반환

Single

변환된 바이트를 나타내는 단정밀도 부동 소수점 값입니다.

예외

value의 길이가 Single 값의 길이보다 작습니다.

적용 대상

ToSingle(Byte[], Int32)

4바이트에서 변환된 단정밀도 부동 소수점 숫자를 바이트 배열의 지정된 위치에 반환합니다.

public:
 static float ToSingle(cli::array <System::Byte> ^ value, int startIndex);
public static float ToSingle (byte[] value, int startIndex);
static member ToSingle : byte[] * int -> single
Public Shared Function ToSingle (value As Byte(), startIndex As Integer) As Single

매개 변수

value
Byte[]

바이트 배열입니다.

startIndex
Int32

value 내의 시작 위치입니다.

반환

Single

startIndex에서 시작하고 4바이트로 형성된 단정밀도 부동 소수점 숫자입니다.

예외

startIndexvalue의 길이에서 3을 뺀 값보다 크거나 같고 value의 길이에서 1을 뺀 값보다 작거나 같은 경우

value이(가) null인 경우

startIndex가 0보다 작거나 value - 1의 길이보다 큰 경우

예제

다음 코드 예제에서는 배열의 Byte 요소를 메서드를 사용하여 Single 값으로 ToSingle 변환합니다.

// Example of the BitConverter::ToSingle method.
using namespace System;

// Convert four byte array elements to a float and display it.
void BAToSingle( array<unsigned char>^bytes, int index )
{
   float value = BitConverter::ToSingle( bytes, index );
   Console::WriteLine( "{0,5}{1,17}{2,18:E7}", index, BitConverter::ToString( bytes, index, 4 ), value );
}


// Display a byte array, using multiple lines if necessary.
void WriteMultiLineByteArray( array<unsigned char>^bytes )
{
   const int rowSize = 20;
   int iter;
   Console::WriteLine( "initial unsigned char array" );
   Console::WriteLine( "---------------------------" );
   for ( iter = 0; iter < bytes->Length - rowSize; iter += rowSize )
   {
      Console::Write( BitConverter::ToString( bytes, iter, rowSize ) );
      Console::WriteLine( "-" );

   }
   Console::WriteLine( BitConverter::ToString( bytes, iter ) );
   Console::WriteLine();
}

int main()
{
   array<unsigned char>^byteArray = {0,0,0,0,128,63,0,0,112,65,0,255,127,71,0,0,128,59,0,0,128,47,73,70,131,5,75,6,158,63,77,6,158,63,80,6,158,63,30,55,190,121,255,255,127,255,255,127,127,1,0,0,0,192,255,0,0,128,255,0,0,128,127};
   Console::WriteLine( "This example of the BitConverter::ToSingle( unsigned "
   "char[ ], int ) \nmethod generates the following output. It "
   "converts elements of a \nbyte array to float values.\n" );
   WriteMultiLineByteArray( byteArray );
   Console::WriteLine( "{0,5}{1,17}{2,18:E7}", "index", "array elements", "float" );
   Console::WriteLine( "{0,5}{1,17}{2,18:E7}", "-----", "--------------", "-----" );
   
   // Convert byte array elements to float values.
   BAToSingle( byteArray, 0 );
   BAToSingle( byteArray, 2 );
   BAToSingle( byteArray, 6 );
   BAToSingle( byteArray, 10 );
   BAToSingle( byteArray, 14 );
   BAToSingle( byteArray, 18 );
   BAToSingle( byteArray, 22 );
   BAToSingle( byteArray, 26 );
   BAToSingle( byteArray, 30 );
   BAToSingle( byteArray, 34 );
   BAToSingle( byteArray, 38 );
   BAToSingle( byteArray, 42 );
   BAToSingle( byteArray, 45 );
   BAToSingle( byteArray, 49 );
   BAToSingle( byteArray, 51 );
   BAToSingle( byteArray, 55 );
   BAToSingle( byteArray, 59 );
}

/*
This example of the BitConverter::ToSingle( unsigned char[ ], int )
method generates the following output. It converts elements of a
byte array to float values.

initial unsigned char array
---------------------------
00-00-00-00-80-3F-00-00-70-41-00-FF-7F-47-00-00-80-3B-00-00-
80-2F-49-46-83-05-4B-06-9E-3F-4D-06-9E-3F-50-06-9E-3F-1E-37-
BE-79-FF-FF-7F-FF-FF-7F-7F-01-00-00-00-C0-FF-00-00-80-FF-00-
00-80-7F

index   array elements             float
-----   --------------             -----
    0      00-00-00-00    0.0000000E+000
    2      00-00-80-3F    1.0000000E+000
    6      00-00-70-41    1.5000000E+001
   10      00-FF-7F-47    6.5535000E+004
   14      00-00-80-3B    3.9062500E-003
   18      00-00-80-2F    2.3283064E-010
   22      49-46-83-05    1.2345000E-035
   26      4B-06-9E-3F    1.2345671E+000
   30      4D-06-9E-3F    1.2345673E+000
   34      50-06-9E-3F    1.2345676E+000
   38      1E-37-BE-79    1.2345679E+035
   42      FF-FF-7F-FF   -3.4028235E+038
   45      FF-FF-7F-7F    3.4028235E+038
   49      01-00-00-00    1.4012985E-045
   51      00-00-C0-FF               NaN
   55      00-00-80-FF         -Infinity
   59      00-00-80-7F          Infinity
*/
// Example of the BitConverter.ToSingle method.
using System;

class BytesToSingleDemo
{
    const string formatter = "{0,5}{1,17}{2,18:E7}";

    // Convert four byte array elements to a float and display it.
    public static void BAToSingle( byte[ ] bytes, int index )
    {
        float value = BitConverter.ToSingle( bytes, index );

        Console.WriteLine( formatter, index,
            BitConverter.ToString( bytes, index, 4 ), value );
    }

    // Display a byte array, using multiple lines if necessary.
    public static void WriteMultiLineByteArray( byte[ ] bytes )
    {
        const int rowSize = 20;
        int iter;

        Console.WriteLine( "initial byte array" );
        Console.WriteLine( "------------------" );

        for( iter = 0; iter < bytes.Length - rowSize; iter += rowSize )
        {
            Console.Write(
                BitConverter.ToString( bytes, iter, rowSize ) );
            Console.WriteLine( "-" );
        }

        Console.WriteLine( BitConverter.ToString( bytes, iter ) );
        Console.WriteLine( );
    }

    public static void Main( )
    {
        byte[ ] byteArray = {
              0,   0,   0,   0, 128,  63,   0,   0, 112,  65,
              0, 255, 127,  71,   0,   0, 128,  59,   0,   0,
            128,  47,  73,  70, 131,   5,  75,   6, 158,  63,
             77,   6, 158,  63,  80,   6, 158,  63,  30,  55,
            190, 121, 255, 255, 127, 255, 255, 127, 127,   1,
              0,   0,   0, 192, 255,   0,   0, 128, 255,   0,
              0, 128, 127 };

        Console.WriteLine(
            "This example of the BitConverter.ToSingle( byte( ), " +
            "int ) \nmethod generates the following output. It " +
            "converts elements \nof a byte array to float values.\n" );

        WriteMultiLineByteArray( byteArray );

        Console.WriteLine( formatter, "index", "array elements",
            "float" );
        Console.WriteLine( formatter, "-----", "--------------",
            "-----" );

        // Convert byte array elements to float values.
        BAToSingle( byteArray, 0 );
        BAToSingle( byteArray, 2 );
        BAToSingle( byteArray, 6 );
        BAToSingle( byteArray, 10 );
        BAToSingle( byteArray, 14 );
        BAToSingle( byteArray, 18 );
        BAToSingle( byteArray, 22 );
        BAToSingle( byteArray, 26 );
        BAToSingle( byteArray, 30 );
        BAToSingle( byteArray, 34 );
        BAToSingle( byteArray, 38 );
        BAToSingle( byteArray, 42 );
        BAToSingle( byteArray, 45 );
        BAToSingle( byteArray, 49 );
        BAToSingle( byteArray, 51 );
        BAToSingle( byteArray, 55 );
        BAToSingle( byteArray, 59 );
    }
}

/*
This example of the BitConverter.ToSingle( byte( ), int )
method generates the following output. It converts elements
of a byte array to float values.

initial byte array
------------------
00-00-00-00-80-3F-00-00-70-41-00-FF-7F-47-00-00-80-3B-00-00-
80-2F-49-46-83-05-4B-06-9E-3F-4D-06-9E-3F-50-06-9E-3F-1E-37-
BE-79-FF-FF-7F-FF-FF-7F-7F-01-00-00-00-C0-FF-00-00-80-FF-00-
00-80-7F

index   array elements             float
-----   --------------             -----
    0      00-00-00-00    0.0000000E+000
    2      00-00-80-3F    1.0000000E+000
    6      00-00-70-41    1.5000000E+001
   10      00-FF-7F-47    6.5535000E+004
   14      00-00-80-3B    3.9062500E-003
   18      00-00-80-2F    2.3283064E-010
   22      49-46-83-05    1.2345000E-035
   26      4B-06-9E-3F    1.2345671E+000
   30      4D-06-9E-3F    1.2345673E+000
   34      50-06-9E-3F    1.2345676E+000
   38      1E-37-BE-79    1.2345679E+035
   42      FF-FF-7F-FF   -3.4028235E+038
   45      FF-FF-7F-7F    3.4028235E+038
   49      01-00-00-00    1.4012985E-045
   51      00-00-C0-FF               NaN
   55      00-00-80-FF         -Infinity
   59      00-00-80-7F          Infinity
*/
open System

let print obj1 obj2 obj3 = printfn $"{obj1,5}{obj2,17}{obj3,18:E7}"

// Convert four byte array elements to a float and display it.
let BAToSingle bytes index =
    let value = BitConverter.ToSingle(bytes, index)

    print index (BitConverter.ToString(bytes, index, 4)) value

// Display a byte array, using multiple lines if necessary.
let writeMultiLineByteArray (bytes: byte []) =
    let rowSize = 20

    printfn "initial byte array"
    printfn "------------------"

    let mutable iter = 0
    for i in 0 .. rowSize .. (bytes.Length - rowSize - 1) do
        printfn $"{BitConverter.ToString(bytes, iter, rowSize)}-"
        iter <- i

    printfn $"{BitConverter.ToString(bytes, iter + rowSize)}\n"

let byteArray =
    [| 0uy; 0uy; 0uy; 0uy; 128uy; 63uy; 0uy; 0uy; 112uy; 65uy
       0uy; 255uy; 127uy; 71uy; 0uy; 0uy; 128uy; 59uy; 0uy; 0uy
       128uy; 47uy; 73uy; 70uy; 131uy; 5uy; 75uy; 6uy; 158uy; 63uy
       77uy; 6uy; 158uy; 63uy; 80uy; 6uy; 158uy; 63uy; 30uy; 55uy
       190uy; 121uy; 255uy; 255uy; 127uy; 255uy; 255uy; 127uy; 127uy; 1uy
       0uy; 0uy; 0uy; 192uy; 255uy; 0uy; 0uy; 128uy; 255uy; 0uy
       0uy; 128uy; 127uy |]

printfn "This example of the BitConverter.ToSingle(byte [], int) \nmethod generates the following output. It converts elements \nof a byte array to float values.\n"

writeMultiLineByteArray byteArray

print "index" "array elements" "float"
print "-----" "--------------" "-----"

// Convert byte array elements to float values.
BAToSingle byteArray 0 
BAToSingle byteArray 2 
BAToSingle byteArray 6 
BAToSingle byteArray 10 
BAToSingle byteArray 14 
BAToSingle byteArray 18 
BAToSingle byteArray 22 
BAToSingle byteArray 26 
BAToSingle byteArray 30 
BAToSingle byteArray 34 
BAToSingle byteArray 38 
BAToSingle byteArray 42 
BAToSingle byteArray 45 
BAToSingle byteArray 49 
BAToSingle byteArray 51 
BAToSingle byteArray 55 
BAToSingle byteArray 59 


// This example of the BitConverter.ToSingle( byte( ), int )
// method generates the following output. It converts elements
// of a byte array to float values.
//
// initial byte array
// ------------------
// 00-00-00-00-80-3F-00-00-70-41-00-FF-7F-47-00-00-80-3B-00-00-
// 00-00-00-00-80-3F-00-00-70-41-00-FF-7F-47-00-00-80-3B-00-00-
// 80-2F-49-46-83-05-4B-06-9E-3F-4D-06-9E-3F-50-06-9E-3F-1E-37-
// 00-80-7F
//
// index   array elements             float
// -----   --------------             -----
//     0      00-00-00-00    0.0000000E+000
//     2      00-00-80-3F    1.0000000E+000
//     6      00-00-70-41    1.5000000E+001
//    10      00-FF-7F-47    6.5535000E+004
//    14      00-00-80-3B    3.9062500E-003
//    18      00-00-80-2F    2.3283064E-010
//    22      49-46-83-05    1.2345000E-035
//    26      4B-06-9E-3F    1.2345671E+000
//    30      4D-06-9E-3F    1.2345673E+000
//    34      50-06-9E-3F    1.2345676E+000
//    38      1E-37-BE-79    1.2345679E+035
//    42      FF-FF-7F-FF   -3.4028235E+038
//    45      FF-FF-7F-7F    3.4028235E+038
//    49      01-00-00-00    1.4012985E-045
//    51      00-00-C0-FF               NaN
//    55      00-00-80-FF                -∞
//    59      00-00-80-7F                 ∞
' Example of the BitConverter.ToSingle method.
Module BytesToSingleDemo

    Const formatter As String = "{0,5}{1,17}{2,18:E7}"
 
    ' Convert four Byte array elements to a Single and display it.
    Sub BAToSingle( bytes( ) As Byte, index As Integer )

        Dim value As Single = BitConverter.ToSingle( bytes, index )

        Console.WriteLine( formatter, index, _
            BitConverter.ToString( bytes, index, 4 ), value )
    End Sub 

    ' Display a Byte array, using multiple lines if necessary.
    Sub WriteMultiLineByteArray( bytes( ) As Byte )
       
        Const rowSize As Integer = 20 
        Dim iter As Integer

        Console.WriteLine( "initial Byte array" )
        Console.WriteLine( "------------------" )

        For iter = 0 To bytes.Length - rowSize - 1 Step rowSize
            Console.Write( _
                BitConverter.ToString( bytes, iter, rowSize ) )
            Console.WriteLine( "-" )
        Next iter

        Console.WriteLine( BitConverter.ToString( bytes, iter ) )
        Console.WriteLine( )
    End Sub

    Sub Main( )
        Dim byteArray as Byte( ) = { _
              0,   0,   0,   0, 128,  63,   0,   0, 112,  65, _
              0, 255, 127,  71,   0,   0, 128,  59,   0,   0, _
            128,  47,  73,  70, 131,   5,  75,   6, 158,  63, _
             77,   6, 158,  63,  80,   6, 158,  63,  30,  55, _
            190, 121, 255, 255, 127, 255, 255, 127, 127,   1, _
              0,   0,   0, 192, 255,   0,   0, 128, 255,   0, _
              0, 128, 127 }

        Console.WriteLine( _
            "This example of the BitConverter.ToSingle( Byte( ), " & _
            "Integer ) " & vbCrLf & "method generates the " & _
            "following output. It converts elements " & vbCrLf & _
            "of a Byte array to Single values." & vbCrLf )

        WriteMultiLineByteArray( byteArray )

        Console.WriteLine( formatter, "index", "array elements", _
            "Single" )
        Console.WriteLine( formatter, "-----", "--------------", _
            "------" )
          
        ' Convert Byte array elements to Single values.
        BAToSingle( byteArray, 0 )
        BAToSingle( byteArray, 2 )
        BAToSingle( byteArray, 6 )
        BAToSingle( byteArray, 10 )
        BAToSingle( byteArray, 14 )
        BAToSingle( byteArray, 18 )
        BAToSingle( byteArray, 22 )
        BAToSingle( byteArray, 26 )
        BAToSingle( byteArray, 30 )
        BAToSingle( byteArray, 34 )
        BAToSingle( byteArray, 38 )
        BAToSingle( byteArray, 42 )
        BAToSingle( byteArray, 45 )
        BAToSingle( byteArray, 49 )
        BAToSingle( byteArray, 51 )
        BAToSingle( byteArray, 55 )
        BAToSingle( byteArray, 59 )
    End Sub 
End Module

' This example of the BitConverter.ToSingle( Byte( ), Integer )
' method generates the following output. It converts elements
' of a Byte array to Single values.
' 
' initial Byte array
' ------------------
' 00-00-00-00-80-3F-00-00-70-41-00-FF-7F-47-00-00-80-3B-00-00-
' 80-2F-49-46-83-05-4B-06-9E-3F-4D-06-9E-3F-50-06-9E-3F-1E-37-
' BE-79-FF-FF-7F-FF-FF-7F-7F-01-00-00-00-C0-FF-00-00-80-FF-00-
' 00-80-7F
' 
' index   array elements            Single
' -----   --------------            ------
'     0      00-00-00-00    0.0000000E+000
'     2      00-00-80-3F    1.0000000E+000
'     6      00-00-70-41    1.5000000E+001
'    10      00-FF-7F-47    6.5535000E+004
'    14      00-00-80-3B    3.9062500E-003
'    18      00-00-80-2F    2.3283064E-010
'    22      49-46-83-05    1.2345000E-035
'    26      4B-06-9E-3F    1.2345671E+000
'    30      4D-06-9E-3F    1.2345673E+000
'    34      50-06-9E-3F    1.2345676E+000
'    38      1E-37-BE-79    1.2345679E+035
'    42      FF-FF-7F-FF   -3.4028235E+038
'    45      FF-FF-7F-7F    3.4028235E+038
'    49      01-00-00-00    1.4012985E-045
'    51      00-00-C0-FF               NaN
'    55      00-00-80-FF         -Infinity
'    59      00-00-80-7F          Infinity

설명

이 메서드는 ToSingle 바이트를 인덱 startIndex 스에서 + 3으로 startIndex 값으로 Single 변환합니다. 배열의 바이트 순서는 컴퓨터 시스템 아키텍처의 엔디언성을 반영해야 합니다. 자세한 내용은 의 설명 섹션 BitConverter을 참조하세요.

추가 정보

적용 대상