I've tried to make m complimentary to ATP's functions.
These work for (large) negative amounts too,
interpretation of negative binary strings <255 is always a gamble..
my rule is: negatives binTodec must have a lenght of multiple8 +2
(and start with 11)
Function DecToBin(ByVal Number As Long, Optional Places As Integer) As
String
Dim neg%, s$
neg = Number < 0
Number = Abs(Number) + neg
Do
s = (Number - neg) Mod 2 & s
Number = Number \ 2
Loop While Number 0
If Places < Len(s) Then
Places = Len(s)
End If
'Length s/b Multiple of 8
Places = Places - (Places - 1) Mod 8 + 7
s = String$(Places - Len(s), CStr(CInt(-neg))) & s
If neg Then
s = "11" & s
End If
DecToBin = s
End Function
Function BinToDec(ByVal BinString As String) As Long
Dim neg%, l&, i%, n%
n = Len(BinString)
neg = n 8 And n Mod 8 = 2
If neg Then
BinString = Mid(BinString, 3)
n = n - 2
End If
For i = 0 To n - 1
If Mid(BinString, n - i, 1) < CInt(-neg) Then
l = l + 2 ^ i
End If
Next
BinToDec = (neg - Not neg) * l + neg
End Function
--
keepITcool
|
www.XLsupport.com | keepITcool chello nl | amsterdam
Jo wrote :
thanks, how about bin to dec?
"keepITcool" wrote:
dont be hasty when posting.. it just takes some time
for the post to filter thru...
see my answer to your first attempt!
--
keepITcool
www.XLsupport.com | keepITcool chello nl | amsterdam
Jo wrote :
Hi
Is there something analagous to dec2bin (worksheet) for
VBA?
I need to change a 5 digit decimal into a 16 character
binary expression.
Thanks
Jo
.