I have been searching for a way to convert a decimal value to a binary string, but thus far have been unsuccessful. I have found many ways to convert an Integer to binary (How to convert a decimal number to a binary number with fixed bits, How to convert from decimal to binary .NET for example), but I want to be able to convert decimal values (2.5, 2374098.034286197548, etc.). Is there a way, or is there not a way to convert actual decimal values?
Asked
Active
Viewed 2,707 times
2
-
I don't know of anything built-in, no... would you want it as binary with a binary point, e.g. "10.1" for (decimal) 2.5? – Jon Skeet Mar 07 '16 at 15:26
-
I'm not sure how many Bits I would need for it but i was wanting it in Byte format binary (0000 0000, 0000 0001). – ARidder101 Mar 07 '16 at 15:28
-
1`Decimal.GetBits()` ? – Alex K. Mar 07 '16 at 15:29
-
Maybe i read the description of `GetBits()` wrong when I was researching, I'll look at that again. – ARidder101 Mar 07 '16 at 15:30
-
1I'm really not sure what that (0000 0000, 0000 0001) is meant to be for. You should edit your question to be *much* clearer... – Jon Skeet Mar 07 '16 at 15:30
-
I thought it was fairly clear with the integer conversion examples but I'll try to clarify. I am trying to figure out if there is a way to create a binary string for decimal values. Something similar to how a byte converts to an 8 bit binary string and a short to a 16 bit string and an integer to a 32 bit string and long to a 64 bit string. – ARidder101 Mar 07 '16 at 15:37
-
I am struggling to see how this could be of any use. Is it just a proof of concept? – Steve Mar 07 '16 at 15:39
-
It's a combination of proof of concept for my sake and in the future I plan to use it for a data logger. – ARidder101 Mar 07 '16 at 15:43
-
How do you plan to display the decimal places? What string would e.g. `18.91`? – Jürgen Steinblock Mar 07 '16 at 15:53
-
I am assuming you are asking about precision? The displayed decimal will never be beyond 2 precision but the scale may vary. Values would likely range from 0.01 to 999.99. – ARidder101 Mar 07 '16 at 16:03
-
@ARidder101 here you go **[Covert Decimal to Binary](https://support.microsoft.com/en-us/kb/109260)** – Trevor Mar 07 '16 at 16:36
-
That one, like the 2 examples of what I don't want above, does not appear to support actual decimal values. It converts decimal format Integers. Thanks for trying though. – ARidder101 Mar 07 '16 at 16:40
1 Answers
0
It seems to me that there is no real proper way to convert numbers with decimal positions (1.03, 234.65) so I decided to make something messy to work it out.
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
This takes the numbers before and after the decimal separately and converts them based on the smallest integer type they fit into. This can then be saved as desired and converted back based on a reverse of your desired save method.
My custom extensions:
''' <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
It isn't very pretty, but I think it will work well enough for me.
Results of use:
100.13 - 01100100 and 00001101
52000.500 - 00000000000000001100101100100000 and 0000000111110100
It needs some work, but this is what I came up with. If anyone can find some improvements feel free to let me know.

Community
- 1
- 1

ARidder101
- 315
- 2
- 6
- 16