ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Worksheet Functions (https://www.excelbanter.com/excel-worksheet-functions/)
-   -   Convert Decimal fractions to binary and vice versa (https://www.excelbanter.com/excel-worksheet-functions/450201-convert-decimal-fractions-binary-vice-versa.html)

[email protected]

Convert Decimal fractions to binary and vice versa
 
Hello all,

How can I convert a decimal fraction (for example 10.5) to binary form and vice versa (i.e., if I have a binary form such as 10010.011 back to decimal form)?

The DEC2BIN function in Excel works for Whole numbers and not fractions.

Please suggest!!

GS[_2_]

Convert Decimal fractions to binary and vice versa
 
On 07/11/2014 11:13 AM, wrote:
Hello all,

How can I convert a decimal fraction (for example 10.5) to binary form and vice versa (i.e., if I have a binary form such as 10010.011 back to decimal form)?

The DEC2BIN function in Excel works for Whole numbers and not fractions.

Please suggest!!

Here's a solution posted by Rick Rothstein some years ago...

Function DecToBin(ByVal DecimalIn As Variant, Optional NumberOfBits As
Variant) As String
' The DecimalIn argument is limited to 79228162514264337593543950266
' (approximately 96-bits) - large numerical values must be entered
' as a String value to prevent conversion to scientific notation.

DecToBin = "": DecimalIn = CDec(DecimalIn)
Do While DecimalIn < 0
DecToBin = Trim$(Str$(DecimalIn - 2 * Int(DecimalIn / 2))) & DecToBin
DecimalIn = Int(DecimalIn / 2)
Loop
If Not IsMissing(NumberOfBits) Then
If Len(DecToBin) NumberOfBits Then
DecToBin = "Error - Number too large for bit size"
Else
DecToBin = Right$(String$(NumberOfBits, "0") & DecToBin,
NumberOfBits)
End If
End If
End Function

Function BinToDec(BinaryString As String) As Variant
' BinaryString argument can be a maximum of 96 digits (either 0's or 1's)

Dim x As Integer
Const TwoToThe48 As Variant = 281474976710656#
For x = 0 To Len(BinaryString) - 1
If x 48 Then
BinToDec = CDec(BinToDec) + Val(Mid(BinaryString,
Len(BinaryString) - x, 1)) * TwoToThe48 * CDec(2 ^ (x - 48))
Else
BinToDec = CDec(BinToDec) + Val(Mid(BinaryString,
Len(BinaryString) - x, 1)) * CDec(2 ^ x)
End If
Next
If Len(BinToDec) 10 Then BinToDec = CStr(BinToDec)
End Function


--
-
Garry

Free Usenet access at
http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion

Ron Rosenfeld[_2_]

Convert Decimal fractions to binary and vice versa
 
On Fri, 11 Jul 2014 08:13:36 -0700 (PDT), wrote:

Hello all,

How can I convert a decimal fraction (for example 10.5) to binary form and vice versa (i.e., if I have a binary form such as 10010.011 back to decimal form)?

The DEC2BIN function in Excel works for Whole numbers and not fractions.

Please suggest!!


Search the internet for the X-Numbers add-in. The BaseChange function will do what you want. And there are many other functions which you may find of interest.


All times are GMT +1. The time now is 12:17 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com