Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Dec2Bin Function Max length of 16 bits

I am using the following function to convert from deciminal to binary.
It works great for small numbers but when I send it a decimal value
that I want convered to 32 binary digits the result that comes out is
only 16 binary bits.

Is there a way to make this function support 32 bit binary? Or is
there another function I can replace it with?



Function Dec2Bin(ByVal DecimalIn As Variant, _
Optional NumberOfBits As Variant) As String
Dec2Bin = ""
DecimalIn = Int(CDec(DecimalIn))
Do While DecimalIn < 0
Dec2Bin = Format$(DecimalIn - 2 * Int(DecimalIn / 2)) &
Dec2Bin
DecimalIn = Int(DecimalIn / 2)
Loop
If Not IsMissing(NumberOfBits) Then
If Len(Dec2Bin) NumberOfBits Then
Dec2Bin = "Error - Number exceeds specified bit size"
Else
Dec2Bin = Right$(String$(NumberOfBits, _
"0") & Dec2Bin, NumberOfBits)
End If
End If
End Function
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Dec2Bin Function Max length of 16 bits

? Dec2Bin(4294967294,32)
11111111111111111111111111111110
? dec2Bin(4294967295,32)
11111111111111111111111111111111
? dec2bin(16,32)
00000000000000000000000000010000

It worked fine for me.

--
Regards,
Tom Ogilvy

"Rich" wrote in message
om...
I am using the following function to convert from deciminal to binary.
It works great for small numbers but when I send it a decimal value
that I want convered to 32 binary digits the result that comes out is
only 16 binary bits.

Is there a way to make this function support 32 bit binary? Or is
there another function I can replace it with?



Function Dec2Bin(ByVal DecimalIn As Variant, _
Optional NumberOfBits As Variant) As String
Dec2Bin = ""
DecimalIn = Int(CDec(DecimalIn))
Do While DecimalIn < 0
Dec2Bin = Format$(DecimalIn - 2 * Int(DecimalIn / 2)) &
Dec2Bin
DecimalIn = Int(DecimalIn / 2)
Loop
If Not IsMissing(NumberOfBits) Then
If Len(Dec2Bin) NumberOfBits Then
Dec2Bin = "Error - Number exceeds specified bit size"
Else
Dec2Bin = Right$(String$(NumberOfBits, _
"0") & Dec2Bin, NumberOfBits)
End If
End If
End Function



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
3 bits of data BimboUK Excel Discussion (Misc queries) 2 June 16th 09 03:19 AM
What 'value' is a Dec2Bin output? EvilTony Excel Discussion (Misc queries) 12 April 13th 09 01:59 PM
=Dec2bin can you increase the number bits please WAT Excel Discussion (Misc queries) 0 July 29th 06 01:20 AM
How to get leading zeros using DEC2BIN scallyte Excel Worksheet Functions 2 December 8th 05 11:52 PM
Base number functions, example DEC2BIN Wayne Boswell via OfficeKB.com Setting up and Configuration of Excel 0 February 24th 05 04:18 AM


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

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"