[Solved] How do I convert ‘Single’ to binary?


The example expected value you’ve provided is just a straight binary representation of the number, however while probably not the most efficient way if you wanted to get the IEEE-754 representation of the number in binary you could use BitConverter.GetBytes as in the following example:

Sub Main
    Dim i As Int32 = 1361294667
    Console.WriteLine(ObjectAsBinary(i))
    Dim s As Single = 1361294667
    Console.WriteLine(ObjectAsBinary(s))
End Sub

Private Function ObjectAsBinary(o As Object) As String
    Dim bytes = BitConverter.GetBytes(o)
    If BitConverter.IsLittleEndian Then
        Array.Reverse(bytes)
    End If
    Dim result As String = ""
    For Each b In bytes
        result &= Convert.ToString(b, 2).PadLeft(8, "0")
    Next
    Return result
End Function

That code outputs the following:

01010001001000111011010101001011 – Matches your example

01001110101000100100011101101011 – Matches IEEE-754 from IEEE-754 Floating Point Converter

3

solved How do I convert ‘Single’ to binary?