Thread: Binary to Decimal or vise versa

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2012
Posts
2
Rep Power
0

Binary to Decimal or vise versa

I know how to use during decimaltobinary and binarytodecimal function on vb 2010,but i cant figure out the way to output without using vb 2010 function

can anyone send me code on how to create a code for converting binary to decimal and decimal to binary without using vb function please.
2. No Profile Picture
Grumpier old Moderator
Devshed Supreme Being (6500+ posts)

Join Date
Jun 2003
Posts
14,460
Rep Power
4539
Why wouldn't you want to use the built-in conversion function in your vb project?
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2012
Posts
2
Rep Power
0
Originally Posted by Doug G
Why wouldn't you want to use the built-in conversion function in your vb project?
because it says on my paper not to use built in function.
can you please tell me if its possible to convert Binary to decimal and decimal to binary without built in fuction and how?
4. Of course it's possible but since it's already done for us we wouldn't bother. Obviously this is intended as an exercise for you to work out. I'd probably look at a C function that does this and convert it to VB.
5. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2011
Posts
289
Rep Power
45
Originally Posted by joyride123
I know how to use during decimaltobinary and binarytodecimal function on vb 2010,but i cant figure out the way to output without using vb 2010 function

can anyone send me code on how to create a code for converting binary to decimal and decimal to binary without using vb function please.
One must assume that you are talking whole numbers here. All numbers are stored in the computer as binary numbers, but each type of number (short integer, long integer, single precision, double precision etc) has it's own storage methodology. So assuming we are dealing with a 16 bit integer:
Private Sub cmdConvert_Click()
Dim N%
Dim Number As Long
Dim sBinary As String
Number = Val(Text1.Text)
sBinary = String(16, "0")
Do Until N% > 15
If Number And 2 ^ N% Then
Mid\$(sBinary, 16 - N%, 1) = "1"
End If
N% = N% + 1
Loop
Text2.Text = sBinary
End Sub
The form would have 2 text boxes and 1 command button. Since most languages don't deal directly with binary numbers, we have to represent it as a binary string. Conversion back would just be the reverse.