簡體   English   中英

在VB.NET中將十進制轉換為二進制

[英]Convert decimal to binary in VB.NET

我一直在尋找一種將十進制值轉換為二進制字符串的方法,但到目前為止仍未成功。 我發現了多種將整數轉換為二進制的方法(例如, 如何將十進制數轉換為具有固定位的二進制數 ,例如如何從十進制轉換為二進制.NET ),但是我希望能夠轉換十進制值( 2.5、23740098.034286197548等)。 有沒有辦法轉換實際的十進制值?

在我看來,沒有真正正確的方法來轉換帶小數位的數字(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

它需要一些工作,但這是我想出的。 如果有人可以找到一些改進,請隨時告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM