Click to See Complete Forum and Search --> : Binaries
Amon Ra
Mar 28th, 2001, 09:15 PM
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?
mlewis
Mar 29th, 2001, 05:25 AM
Check out this thread:
http://forums.vb-world.net/showthread.php?threadid=61336&goto=newpost
The question is answered quite well there.
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:
Print Bin("110001010101010001011011")
Amon Ra
Mar 29th, 2001, 08:56 PM
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
Mar 29th, 2001, 09:30 PM
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
mlewis
Apr 1st, 2001, 01:54 AM
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.
mlewis
Apr 1st, 2001, 01:58 AM
Sorry, Meggie, but I just had to say, do your homework.
Amon ra: the code should read as below:
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.
Lord Orwell
Apr 1st, 2001, 04:08 AM
here is something i threw together a few min ago.
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 :D
Amon Ra
Apr 1st, 2001, 11:34 AM
Thanks guys. Now i have to decide which one to use :)
Lord Orwell
Apr 1st, 2001, 03:17 PM
you have to use mine!
Im the only one who gave a converstion TO binary.
;) :D
converting FROM is the easy part...
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)
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).
Print Bin("00000101")
Amon Ra
Apr 1st, 2001, 08:54 PM
OK, your functions all work :) Thx
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.