2016-03-07 162 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

它需要一些工作,但是这是我想出了。如果任何人都可以找到一些改进,随时让我知道。

相关问题