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
-
Unable to update to version 22h2 (Awaiting moderation)
by
04om
17 minutes ago -
Windows 11 Insider Preview Build 26100.4482 (24H2) released to Release Preview
by
joep517
6 hours, 54 minutes ago -
Windows 11 Insider Preview build 27881 released to Canary
by
joep517
6 hours, 56 minutes ago -
Very Quarrelsome Taskbar!
by
CWBillow
17 hours, 9 minutes ago -
Move OneNote Notebook OFF OneDrive and make it local
by
CWBillow
19 hours, 53 minutes ago -
Microsoft 365 to block file access via legacy auth protocols by default
by
Alex5723
8 hours, 38 minutes ago -
Is your battery draining?
by
Susan Bradley
1 hour, 45 minutes ago -
The 16-billion-record data breach that no one’s ever heard of
by
Alex5723
2 hours, 38 minutes ago -
Weasel Words Rule Too Many Data Breach Notifications
by
Nibbled To Death By Ducks
23 hours, 45 minutes ago -
Windows Command Prompt and Powershell will not open as Administrator
by
Gordski
7 hours, 50 minutes ago -
Intel Management Engine (Intel ME) Security Issue
by
PL1
8 hours, 2 minutes ago -
Old Geek Forced to Update. Buy a Win 11 PC? Yikes! How do I cope?
by
RonE22
42 minutes ago -
National scam day
by
Susan Bradley
7 hours, 35 minutes ago -
macOS Tahoe 26 the end of the road for Intel Macs, OCLP, Hackintosh
by
Alex5723
3 hours, 44 minutes ago -
Cyberattack on some Washington Post journalists’ email accounts
by
Bob99
2 days ago -
Tools to support internet discussions
by
Kathy Stevens
13 hours, 30 minutes ago -
How get Group Policy to allow specific Driver to download?
by
Tex265
1 day, 15 hours ago -
AI is good sometimes
by
Susan Bradley
2 days, 8 hours ago -
Mozilla quietly tests Perplexity AI as a New Firefox Search Option
by
Alex5723
1 day, 22 hours ago -
Perplexity Pro free for 12 mos for Samsung Galaxy phones
by
Patricia Grace
3 days, 8 hours ago -
June KB5060842 update broke DHCP server service
by
Alex5723
3 days, 6 hours ago -
AMD Ryzen™ Chipset Driver Release Notes 7.06.02.123
by
Alex5723
3 days, 10 hours ago -
Excessive security alerts
by
WSSebastian42
2 days, 1 hour ago -
* CrystalDiskMark may shorten SSD/USB Memory life
by
Alex5723
3 days, 20 hours ago -
Ben’s excellent adventure with Linux
by
Ben Myers
10 hours, 33 minutes ago -
Seconds are back in Windows 10!
by
Susan Bradley
3 days, 7 hours ago -
WebBrowserPassView — Take inventory of your stored passwords
by
Deanna McElveen
2 days, 1 hour ago -
OS news from WWDC 2025
by
Will Fastie
1 day, 11 hours ago -
Need help with graphics…
by
WSBatBytes
2 days, 15 hours ago -
AMD : Out of Bounds (OOB) read vulnerability in TPM 2.0 CVE-2025-2884
by
Alex5723
4 days, 11 hours 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.