|
-
Mar 28th, 2001, 10:15 PM
#1
Thread Starter
Hyperactive Member
Urgent. How do i convert a decimal to binaries and vice versa? Also, how can i fiund the high and low bit of the binary?
Amon Ra
The Power of Learning.
-
Mar 29th, 2001, 06:25 AM
#2
Frenzied Member
Check out this thread:
http://forums.vb-world.net/showthrea...6&goto=newpost
The question is answered quite well there.
-
Mar 29th, 2001, 03:36 PM
#3
Code:
Function Bin(ByVal sBinary As String) As Long
Dim I As Integer
For I = 0 To Len(sBinary) - 1
If Mid(sBinary, Len(sBinary) - I, 1) = "1" Then Bin = Bin + (2 ^ I)
Next
End Function
Usage:
Code:
Print Bin("110001010101010001011011")
-
Mar 29th, 2001, 09:56 PM
#4
Thread Starter
Hyperactive Member
ok
in the following:
code:--------------------------------------------------------------------------------
1-Private Function Dec2Bin(Num As Integer) As String
2- Dim i As Integer
3- Dec2Bin = ""
4- For i = 0 To 7
5- If Num And 2 ^ i Then
6- Dec2Bin = "1" + Dec2Bin
7- Else
8- Dec2Bin = "0" + Dec2Bin
9- End If
10- Next i
End Function
what does the line 5 do?
Amon Ra
The Power of Learning.
-
Mar 29th, 2001, 10:30 PM
#5
Thread Starter
Hyperactive Member
Also
the function you, megatron, gave me does not work for me 
if i convert 2 to binaries, it returns 00000010
but if i try to go from bin. to dec. with your function with
00000101, it returns 14 instead of 5 . i dont get it
Amon Ra
The Power of Learning.
-
Mar 30th, 2001, 03:53 PM
#6
-
Apr 1st, 2001, 02:54 AM
#7
Frenzied Member
OK, in decimal:
1234 is broken up like this: 1 thousand, 2 hundreds, 3 tens, and 4 ones. In binary, it works like this:
1111 is 1 one, 1 two, 1 four, and 1 eight, or 15, while 1110 is no ones, 1 two, 1 four, and 1 eight, or 14. Line 5 in that code checks to see whether the next digit it needs to write is a 0 or a 1.
-
Apr 1st, 2001, 02:58 AM
#8
Frenzied Member
Megatrons function
Sorry, Meggie, but I just had to say, do your homework.
Amon ra: the code should read as below:
Code:
Public Function Bin2Dec(Byval Bin As String) As Long
Dim I As Integer, RetVal As Long
For I=Len(Bin) To 1 Step -1
If Mid$(Bin, I, 1)="1" Then RetVal=RetVal+(2^(I-1))
Next I
End Function
This should work (quite) a bit better.
-
Apr 1st, 2001, 04:08 AM
#9
here is something i threw together a few min ago.
Code:
Function dec2bin(numtoconvert As Long) As String
Dim worktext As String
Dim cl As Long
Dim workval As Long
worktext = Oct(numtoconvert)
For cl = 1 To Len(worktext)
workval = Val(Mid(worktext, cl, 1))
If (workval And 4) = 4 Then output = output + "1" Else output = output + "0"
If (workval And 2) = 2 Then output = output + "1" Else output = output + "0"
If (workval And 1) = 1 Then output = output + "1" Else output = output + "0"
Next cl
dec2bin = output
End Function
Function bin2dec(text As String) As Long
Dim multiplier As Double
Dim cl As Long
multiplier = 1
Dim output As Double
For cl = Len(text) To 1 Step -1
output = output + (Val(Mid$(text, cl, 1)) * multiplier)
multiplier = multiplier + multiplier
Next cl
bin2dec = output
End Function
I hope you don't have any problem decoding HOW they work. Just be happy that they do work
Last edited by Lord Orwell; Apr 1st, 2001 at 04:26 AM.
-
Apr 1st, 2001, 11:34 AM
#10
Thread Starter
Hyperactive Member
:)
Thanks guys. Now i have to decide which one to use
Amon Ra
The Power of Learning.
-
Apr 1st, 2001, 03:17 PM
#11
you have to use mine!
Im the only one who gave a converstion TO binary.

converting FROM is the easy part...
-
Apr 1st, 2001, 03:34 PM
#12
Re: Megatrons function
Originally posted by mlewis
Sorry, Meggie, but I just had to say, do your homework.
I beg to differ, but my function does work.
Try this (in a Module)
Code:
Function Bin(ByVal sBinary As String) As Long
Dim I As Integer
For I = 0 To Len(sBinary) - 1
If Mid(sBinary, Len(sBinary) - I, 1) = "1" Then Bin = Bin + (2 ^ I)
Next
End Function
Now place this line in a CommandButton and it outputs 5 (not 14).
Code:
Print Bin("00000101")
-
Apr 1st, 2001, 08:54 PM
#13
Thread Starter
Hyperactive Member
HAHAHA
OK, your functions all work Thx
Amon Ra
The Power of Learning.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|