Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 1
Default 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!!
  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 3,514
Default 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
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
I need to convert foot pounds of energy to joules and vice versa Sid Excel Worksheet Functions 6 January 28th 09 11:37 PM
Hexadecimal to Decimal (and vice versa) Toria Excel Worksheet Functions 5 January 22nd 09 09:43 PM
Function To Convert Liters To Imperial Gallons & Vice Versa Dave Gibson Excel Worksheet Functions 3 September 24th 05 09:03 PM
Formula to convert/transpose columns to rows (and vice versa) markx Excel Worksheet Functions 5 March 10th 05 02:18 PM


All times are GMT +1. The time now is 02:01 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"