2016-03-07 164 views
2

我一直在尋找一種方法將十進制值轉換爲二進制字符串,但迄今爲止一直不成功。我發現很多方法將整數轉換爲二進制(例如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等)。有沒有辦法,還是沒有辦法來轉換實際的十進制值?在VB.NET中將十進制轉換爲二進制

+0

我不知道任何內置的,不......你是否希望它作爲二進制的二進制點,例如(十進制)2.5爲「10.1」 –

+0

我不知道有多少位需要它,但我想要它在字節格式二進制(0000 0000,0000 0001)。 – ARidder101

+1

'Decimal.GetBits()'? –

回答

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

它需要一些工作,但是這是我想出了。如果任何人都可以找到一些改進,隨時讓我知道。

相關問題