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?
Printable View
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?
Check out this thread:
http://forums.vb-world.net/showthrea...6&goto=newpost
The question is answered quite well there.
Usage: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
Code:Print Bin("110001010101010001011011")
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?
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
2 is 00000010 in binary.
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.
Sorry, Meggie, but I just had to say, do your homework.
Amon ra: the code should read as below:
This should work (quite) a bit better.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
here is something i threw together a few min ago.
I hope you don't have any problem decoding HOW they work. Just be happy that they do work :DCode: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
Thanks guys. Now i have to decide which one to use :)
you have to use mine!
Im the only one who gave a converstion TO binary.
;) :D
converting FROM is the easy part...
I beg to differ, but my function does work.Quote:
Originally posted by mlewis
Sorry, Meggie, but I just had to say, do your homework.
Try this (in a Module)
Now place this line in a CommandButton and it outputs 5 (not 14).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
Code:Print Bin("00000101")
OK, your functions all work :) Thx