Library code snippets
Convert Decimal Integer Values to Binary String in VB6
By Alex Etchells, published on 22 Feb 2006
A small function to convert decimal integer values to a binary string. The number of bits can be optionally specified but this will be increased if insufficient.
Public Function DecToBin(DeciValue As Long, Optional NoOfBits As Integer = 8) _
As String
'********************************************************************************
'* Name : DecToBin
'* Date : 2003
'* Author : Alex Etchells
'*********************************************************************************
Dim i As Integer
'make sure there are enough bits to contain the number
Do While DeciValue > (2 ^ NoOfBits) - 1
NoOfBits = NoOfBits + 8
Loop
DecToBin = vbNullString
'build the string
For i = 0 To (NoOfBits - 1)
DecToBin = CStr((DeciValue And 2 ^ i) / 2 ^ i) & DecToBin
Next i
End Function
Related articles
Related discussion
-
add [DOT] to text line
by kayatri (0 replies)
-
Looping in text file
by kayatri (0 replies)
-
need help in designing a media player using vb.net/ocx object
by kensville (0 replies)
-
Please Help Me: Recordset to array
by sidachaba@yahoo.co.uk (0 replies)
-
Creating a Windows Service in VB.NET
by Templario55 (107 replies)
Related podcasts
-
xpert to Expert: Inside Concurrent Basic (CB)
"Concurrent Basic extends Visual Basic with stylish asynchronous concurrency constructs derived from the join calculus. Our design advances earlier MSRC work on Polyphonic C#, Comega and the Joins Library. Unlike its C# based predecessors, CB adopts a simple event-like syntax familiar to VB progr...
Hi DoctorMahdi,
How do I put BS into an array before display it into text2.text. Thank you.
Dim BS As String
Number = Val(Text1.Text)
BS = ""
While Number > 0
BS = Number Mod 2 & BS
Number = Number \ 2
Wend
Text2.Text = BS
or may be a one liner like...
Dim binaryString As String = Convert.ToString(numberToConvert, 2)
Use PadLeft(n, "0") for required minimum length(n) of the output string.
so it would look like
Dim binaryString As String = Convert.ToString(numberToConvert, 2).PadLeft(n, "0")
Prashant
This thread is for discussions of Convert Decimal Integer Values to Binary String in VB.