Hi,
I encountered some weird bug in a function conversing a number to a binary value… can anyone tell me what went wrong? I use two functions
– CBin() is the main procedure converting a double value to a binary one by splitting it into powers of two and recombining these as powers of ten into the right binary number (e.g. 9 = 8 + 1 = 2^3 + 2^0 => 10
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Convert number to/from binary (VBA)
Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Convert number to/from binary (VBA)
- This topic has 8 replies, 3 voices, and was last updated 21 years, 7 months ago.
AuthorTopicWShasse
AskWoody LoungerOctober 30, 2003 at 1:23 am #395806Viewing 1 reply threadAuthorReplies-
WSHansV
AskWoody LoungerOctober 30, 2003 at 2:01 am #737284Double precision floating point numbers have a precision of 15 significant digits. The difference between 1000000000000000 and 1000000000000001 is in the 16th digit, so it gets lost. You can’t represent numbers over 32,767 as binary in a variable of type Double. You’ll have to use strings.
-
WShasse
AskWoody Lounger -
WSMarkD
AskWoody LoungerOctober 30, 2003 at 10:04 am #737436If interested here is an example of a Decimal-to-Binary function that converts a Long Integer value (max value range -2^31 to 2^31 or -2147483648 to 2147483647) to binary number represented by a string:
Public Function DecToBin(ByVal lngDec As Long) As String
Const MAXLEN = 30
Dim strBin As String
Dim n As LongIf lngDec < 0 Then
strBin = "1"
Else
strBin = "0"
End IfFor n = MAXLEN To 0 Step -1
If lngDec And (2 ^ n) Then
strBin = strBin & "1"
Else
strBin = strBin & "0"
End If
NextDecToBin = strBin
End Function
Sample results:
? DecToBin(32768)
00000000000000001000000000000000
? DecToBin(32769)
00000000000000001000000000000001
? DecToBin(2^31-1)
01111111111111111111111111111111
? DecToBin(2147483647)
01111111111111111111111111111111
? DecToBin(-2^31)
10000000000000000000000000000000
? DecToBin(-2147483648)
10000000000000000000000000000000
? DecToBin(2^31)
' Overflow errorFunction based on example in MSKB Art 109260, How to Convert a Decimal Number to a Binary Number in a String modified somewhat. As noted in article, "This program accepts a nine-digit positive decimal number and returns a 32- character string that represents the number in binary notation. Negative numbers are converted into the 32-digit, twos-complement binary format used by long integers in Basic…. In that format, the left-most binary digit (the thirty-second digit in a long integer) will always be 1 for a negative number and 0 for a positive number."
If you don't want the leading zeroes then you'd need to modify function.
HTH
-
WSMarkD
AskWoody LoungerOctober 30, 2003 at 10:04 am #737437If interested here is an example of a Decimal-to-Binary function that converts a Long Integer value (max value range -2^31 to 2^31 or -2147483648 to 2147483647) to binary number represented by a string:
Public Function DecToBin(ByVal lngDec As Long) As String
Const MAXLEN = 30
Dim strBin As String
Dim n As LongIf lngDec < 0 Then
strBin = "1"
Else
strBin = "0"
End IfFor n = MAXLEN To 0 Step -1
If lngDec And (2 ^ n) Then
strBin = strBin & "1"
Else
strBin = strBin & "0"
End If
NextDecToBin = strBin
End Function
Sample results:
? DecToBin(32768)
00000000000000001000000000000000
? DecToBin(32769)
00000000000000001000000000000001
? DecToBin(2^31-1)
01111111111111111111111111111111
? DecToBin(2147483647)
01111111111111111111111111111111
? DecToBin(-2^31)
10000000000000000000000000000000
? DecToBin(-2147483648)
10000000000000000000000000000000
? DecToBin(2^31)
' Overflow errorFunction based on example in MSKB Art 109260, How to Convert a Decimal Number to a Binary Number in a String modified somewhat. As noted in article, "This program accepts a nine-digit positive decimal number and returns a 32- character string that represents the number in binary notation. Negative numbers are converted into the 32-digit, twos-complement binary format used by long integers in Basic…. In that format, the left-most binary digit (the thirty-second digit in a long integer) will always be 1 for a negative number and 0 for a positive number."
If you don't want the leading zeroes then you'd need to modify function.
HTH
-
-
WShasse
AskWoody Lounger
-
-
WSHansV
AskWoody LoungerOctober 30, 2003 at 2:01 am #737285Double precision floating point numbers have a precision of 15 significant digits. The difference between 1000000000000000 and 1000000000000001 is in the 16th digit, so it gets lost. You can’t represent numbers over 32,767 as binary in a variable of type Double. You’ll have to use strings.
Viewing 1 reply thread -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Windows 11 Insider Preview build 27871 released to Canary
by
joep517
14 hours, 8 minutes ago -
Windows 11 ad from Campaign Manager in Windows 10
by
Jim McKenna
11 hours, 28 minutes ago -
Small desktops
by
Susan Bradley
4 hours, 21 minutes ago -
Totally disable Bitlocker
by
CWBillow
12 hours, 29 minutes ago -
Phishers extract Millions from HMRC accounts..
by
Microfix
11 hours, 45 minutes ago -
Windows 10 22H2 Update today (5 June) says up-to-date but last was 2025-04
by
Alan_uk
1 day, 18 hours ago -
Thoughts on Malwarebytes Scam Guard for Mobile?
by
opti1
1 hour, 14 minutes ago -
Mystical Desktop
by
CWBillow
1 day, 21 hours ago -
Meta and Yandex secretly tracked billions of Android users
by
Alex5723
1 day, 2 hours ago -
MS-DEFCON 2: Do you need that update?
by
Susan Bradley
13 hours, 18 minutes ago -
CD/DVD drive is no longer recognized
by
WSCape Sand
2 days, 12 hours ago -
Windows 11 24H2 Default Apps stuck on Edge and Adobe Photoshop
by
MikeBravo
2 days, 15 hours ago -
North Face and Cartier customer data stolen in cyber attacks
by
Alex5723
2 days, 13 hours ago -
What is wrong with simple approach?
by
WSSpoke36
11 hours, 36 minutes ago -
Microsoft-Backed Builder.ai Set for Bankruptcy After Cash Seized
by
Alex5723
3 days ago -
Location, location, location
by
Susan Bradley
1 day, 15 hours ago -
Cannot get a task to run a restore point
by
CWBillow
3 days, 2 hours ago -
Frustrating search behavior with Outlook
by
MrJimPhelps
2 days, 17 hours ago -
June 2025 Office non-Security Updates
by
PKCano
3 days, 13 hours ago -
Secure Boot Update Fails after KB5058405 Installed
by
SteveIT
1 day, 15 hours ago -
Firefox Red Panda Fun Stuff
by
Lars220
3 days, 13 hours ago -
How start headers and page numbers on page 3?
by
Davidhs
3 days, 23 hours ago -
Attack on LexisNexis Risk Solutions exposes data on 300k +
by
Nibbled To Death By Ducks
3 days, 2 hours ago -
Windows 11 Insider Preview build 26200.5622 released to DEV
by
joep517
4 days, 8 hours ago -
Windows 11 Insider Preview build 26120.4230 (24H2) released to BETA
by
joep517
4 days, 8 hours ago -
MS Excel 2019 Now Prompts to Back Up With OneDrive
by
lmacri
3 days, 21 hours ago -
Firefox 139
by
Charlie
3 days, 14 hours ago -
Who knows what?
by
Will Fastie
2 days, 16 hours ago -
My top ten underappreciated features in Office
by
Peter Deegan
19 hours, 24 minutes ago -
WAU Manager — It’s your computer, you are in charge!
by
Deanna McElveen
5 hours, 18 minutes ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.