在VB.NET中将十进制转换为二进制

时间:2016-03-07 15:25:02

标签: vb.net binary

我一直在寻找一种将十进制值转换为二进制字符串的方法,但到目前为止还没有成功。我找到了很多方法将Integer转换为二进制(例如 How to convert a decimal number to a binary number with fixed bits How to convert from decimal to binary .NET ,但我希望能够转换十进制值(2.5,2374098.034286197548等)。有没有办法,或者没有办法转换实际的十进制值?

1 个答案:

答案 0 :(得分:0)

在我看来,没有真正正确的方法来转换带有小数位的数字(1.03,234.65),所以我决定制作一些杂乱的东西来解决它。

        Dim this As String = String.Empty
        For Each Match As Match In Regex.Matches(BoxyBoxington.Text, "\d+")
            If Match.Value.toDecimal <= Byte.MaxValue.toDecimal Then
                this = this + Convert.ToString(Match.Value.toByte, 2).PadLeft(8, "0") + NewLine
            ElseIf Match.Value.toDecimal <= Short.MaxValue.toDecimal Then
                this = this + Convert.ToString(Match.Value.toShort, 2).PadLeft(16, "0") + NewLine
            ElseIf Match.Value.toDecimal <= Integer.MaxValue.toDecimal Then
                this = this + Convert.ToString(Match.Value.toInteger, 2).PadLeft(32, "0") + NewLine
            ElseIf Match.Value.toDecimal <= Long.MaxValue.toDecimal Then
                this = this + Convert.ToString(Match.Value.toLong, 2).PadLeft(64, "0") + NewLine
            Else
                Exit For
            End If
        Next

分别取小数前后的数字,并根据它们所适合的最小整数类型转换它们。然后可以根据需要保存它,并根据所需保存方法的反向转换回来。

我的自定义扩展程序:

''' <summary>
''' Handles conversion of variable into Long Integer.
''' </summary>
''' <param name="X"></param>
''' <param name="I">Returned if conversion fails.</param>
''' <returns>Signed 64bit Integer</returns>
''' <remarks></remarks>
<Extension> _
Public Function toLong(Of T)(ByRef X As T, Optional I As Long = 0) As Long
    Dim S As String = X.ToString
    Try
        If S = String.Empty Then
            Return I
        Else
            Return Long.Parse(S)
        End If
    Catch
        Dim result As String = String.Empty
        Dim ReturnLong As Long
        Dim Parsed As Byte
        For Each Character In S.ToCharArray
            If Character = "-" Then
                If S.Substring(0, 1).ToString <> "-" Then
                    result = Character + result
                End If
            End If
            If Character = "." Then
                Exit For
            End If
            If Byte.TryParse(Character, Parsed) Then
                result = result + Parsed.ToString
            End If
        Next
        If result <> String.Empty Then
            If Integer.TryParse(result, ReturnLong) Then
                Return Integer.Parse(ReturnLong)
            Else
                If Double.Parse(result) > Double.Parse(Integer.MaxValue.ToString) Then
                    Return Integer.MaxValue
                ElseIf Double.Parse(result) < Double.Parse(Integer.MinValue.ToString) Then
                    Return Integer.MinValue
                Else
                    Return Integer.Parse(ReturnLong)
                End If
            End If
        Else
            Return I
        End If
    End Try
End Function

''' <summary>
''' Handles conversion of variable to Integer.
''' </summary>
''' <param name="X"></param>
''' <param name="I">Returned if conversion fails.</param>
''' <returns>Signed 32bit Integer</returns>
''' <remarks></remarks>
<Extension> _
Public Function toInteger(Of T)(ByRef X As T, Optional I As Integer = 0) As Integer
    Dim S As String = X.ToString
    Try
        If S = String.Empty Then
            Return I
        Else
            Return Integer.Parse(S)
        End If
    Catch
        Dim result As String = String.Empty
        Dim ReturnInt As Integer
        Dim Parsed As Byte
        For Each Character In S.ToCharArray
            If Character = "-" Then
                If S.Substring(0, 1).ToString <> "-" Then
                    result = Character + result
                End If
            End If
            If Character = "." Then
                Exit For
            End If
            If Byte.TryParse(Character, Parsed) Then
                result = result + Parsed.ToString
            End If
        Next
        If result <> String.Empty Then
            If Integer.TryParse(result, ReturnInt) Then
                Return Integer.Parse(ReturnInt)
            Else
                If Double.Parse(result) > Double.Parse(Integer.MaxValue.ToString) Then
                    Return Integer.MaxValue
                ElseIf Double.Parse(result) < Double.Parse(Integer.MinValue.ToString) Then
                    Return Integer.MinValue
                Else
                    Return Integer.Parse(ReturnInt)
                End If
            End If
        Else
            Return I
        End If
    End Try
End Function

''' <summary>
''' Handles conversion of variable to Short Integer.
''' </summary>
''' <param name="X"></param>
''' <param name="I">Returned if conversion fails.</param>
''' <returns>Signed 16bit Integer</returns>
''' <remarks></remarks>
<Extension> _
Public Function toShort(Of T)(ByRef X As T, Optional I As Short = 0) As Short
    Dim S As String = X.ToString
    Try
        If S = String.Empty Then
            Return I
        Else
            Return Short.Parse(S)
        End If
    Catch
        Dim result As String = String.Empty
        Dim ReturnShort As Short
        Dim Parsed As Byte
        For Each Character In S.ToCharArray
            If Character = "-" Then
                If S.Substring(0, 1).ToString <> "-" Then
                    result = Character + result
                End If
            End If
            If Character = "." Then
                Exit For
            End If
            If Byte.TryParse(Character, Parsed) Then
                result = result + Parsed.ToString
            End If
        Next
        If result <> String.Empty Then
            If Short.TryParse(result, ReturnShort) Then
                Return Short.Parse(ReturnShort)
            Else
                If Integer.Parse(result) > Integer.Parse(Short.MaxValue.ToString) Then
                    Return Short.MaxValue
                ElseIf Integer.Parse(result) < Integer.Parse(Short.MinValue.ToString) Then
                    Return Short.MinValue
                Else
                    Return Short.Parse(ReturnShort)
                End If
            End If
        Else
            Return I
        End If
    End Try
End Function

''' <summary>
''' Handles conversion of variable to Tiny Integer
''' </summary>
''' <param name="X"></param>
''' <param name="I">Returned if conversion fails.</param>
''' <returns>Signed 8bit Integer</returns>
''' <remarks></remarks>
<Extension> _
Public Function toSByte(Of T)(ByRef X As T, Optional I As SByte = 0) As SByte
    Dim S As String = X.ToString
    Try
        If S = String.Empty Then
            Return I
        Else
            Return SByte.Parse(S)
        End If
    Catch
        Dim result As String = String.Empty
        Dim ReturnByte As SByte
        Dim Parsed As Byte
        For Each Character In S.ToCharArray
            If Character = "-" Then
                If S.Substring(0, 1).ToString <> "-" Then
                    result = Character + result
                End If
            End If
            If Character = "." Then
                Exit For
            End If
            If Byte.TryParse(Character, Parsed) Then
                result = result + Parsed.ToString
            End If
        Next
        If result <> String.Empty Then
            If SByte.TryParse(result, ReturnByte) Then
                Return SByte.Parse(ReturnByte)
            Else
                If Short.Parse(result) > Short.Parse(SByte.MaxValue.ToString) Then
                    Return SByte.MaxValue
                ElseIf Short.Parse(result) < Short.Parse(SByte.MinValue.ToString) Then
                    Return SByte.MinValue
                Else
                    Return I
                End If
            End If
        Else
            Return I
        End If
    End Try
End Function

''' <summary>
''' Handles conversion of variable to Tiny Integer
''' </summary>
''' <param name="X"></param>
''' <param name="I">Returned if conversion fails.</param>
''' <returns>Unsigned 8bit Integer</returns>
''' <remarks></remarks>
<Extension> _
Public Function toByte(Of T)(ByRef X As T, Optional I As Byte = 0) As Byte
    Dim S As String = X.ToString
    Try
        If S = String.Empty Then
            Return I
        Else
            Return Byte.Parse(S)
        End If
    Catch
        Dim result As String = String.Empty
        Dim ReturnByte As Byte
        Dim Parsed As Byte
        For Each Character In S.ToCharArray
            If Character = "-" Then
                If S.Substring(0, 1).ToString <> "-" Then
                    Exit For
                End If
            End If
            If Character = "." Then
                Exit For
            End If
            If Byte.TryParse(Character, Parsed) Then
                result = result + Parsed.ToString
            End If
        Next
        If result <> String.Empty Then
            If Byte.TryParse(result, ReturnByte) Then
                Return Byte.Parse(ReturnByte)
            Else
                If Short.Parse(result) > Short.Parse(Byte.MaxValue.ToString) Then
                    Return Byte.MaxValue
                ElseIf Short.Parse(result) < Short.Parse(Byte.MinValue.ToString) Then
                    Return Byte.MinValue
                Else
                    Return I
                End If
            End If
        Else
            Return I
        End If
    End Try
End Function

<Extension> _
Public Function toDecimal(Of T)(X As T) As Decimal
    Dim s As String = X.ToString
    Try
        If s = String.Empty Then
            Return 0
        Else
            Return Decimal.Parse(s)
        End If
    Catch
        Dim result As String = String.Empty
        Dim ReturnDec As Decimal
        Dim Parsed As Byte
        For Each Character In s.ToCharArray
            If Character = "-" Then
                If s.Substring(0, 1).ToString <> "-" Then
                    result = Character + result
                End If
            End If
            If Character = "." Then
                result = result + Character
            End If
            If Byte.TryParse(Character, Parsed) Then
                result = result + Parsed.ToString
            End If
        Next
        If result <> String.Empty Then
            If Decimal.TryParse(result, ReturnDec) Then
                Return Decimal.Parse(ReturnDec)
            Else
                Return 0
            End If
        Else
            Return 0
        End If
    End Try
End Function

它不是很漂亮,但我认为它对我来说效果不错。

使用结果:

  

100.13 - 01100100和00001101

     

52000.500 - 00000000000000001100101100100000和0000000111110100

它需要一些工作,但这是我想出来的。如果有人能找到一些改进,请随时告诉我。