|
-
Mar 11th, 2004, 03:46 PM
#1
Thread Starter
Member
AAAH!!! i'm running out of time!!!
ok so i have this...
VB Code:
Option Explicit
Dim strNewNum As String
Function DecimalToBinary(ByVal lngBin As Long, lngDesc As Long) As Long
Dim intNum As Integer
Dim lngBinary2 As Long
Dim lngDecimal As Long
Dim lngStepping As Long
Dim strDigit As String
intNum = txtNum.Text
lngStepping = 1
Do Until intNum \ lngStepping = 1
lngStepping = lngStepping * 2
Loop
Do Until intNum <= 0
strDigit = intNum \ lngStepping
strNewNum = strNewNum & strDigit
intNum = intNum Mod lngStepping
lngStepping = lngStepping / 2
Loop
End Function
Private Sub cmdBtoD_Click()
End Sub
Private Sub cmdDone_Click()
Unload Me
End Sub
Private Sub cmdDtoB_Click()
Dim lngBinary As Long
lngBinary = DecimalToBinary(1, 65000)
MsgBox (strNewNum)
End Sub
it all works...
now how do you convert the binary back to decimal? (no case plz i havn't learned them)
I'm just a basic visual basic programmer!!! (no seriously... i'm just learning this stuff... the so called 'easy' stuff)... plz don't get mad at me if i don't know what i'm doing!!!
-
Mar 11th, 2004, 03:53 PM
#2
Frenzied Member
Each digit in a binary value represents a power of 2, starting with 2^0 for the first (right-most) binary digit, 2^1 for the second digit, and so on. Note that any number to the 0 power is by definition 1, and any number to the 1 power is the number itself. Using 1101 for another example, you have:
The first digit is 1, and 1 times 2^0 is 1.
The second digit is 0, and 0 times 2^1 is 0.
The third digit is 1, and 1 times 2^2 is 4.
The fourth digit is 1, and 1 times 2^3 is 8.
8 + 4 + 1 equals 13.
A function to perform the binary to decimal conversion is shown here.
VB Code:
Public Function BinaryToDecimal(BinaryValue As String) As Long
' Returns the decimal equivalent of a binary number.
Dim idx As Integer
Dim tmp As String
Dim result As Long
Dim digits As Integer
digits = Len(BinaryValue)
For idx = digits To 1 Step -1
tmp = Mid(BinaryValue, idx, 1)
If tmp = "1" Then result = result + 2 ^ (digits - idx)
Next
BinaryToDecimal = result
End Function
This function treats any character other than "1" in the binary value as a "0". You might want to add error-schecking code to ensure that the binary value contains only "0" and "1" characters.
-
Mar 11th, 2004, 03:59 PM
#3
-
Mar 11th, 2004, 04:03 PM
#4
Thread Starter
Member
new one..
ok here is what i have
VB Code:
Function BinaryToDecimal(ByVal lngBin As Long, ByVal lngDesc As Long) As Long
Dim strNum As String
Dim lngNum As Long
Dim lngCounter As Long
strNum = txtNum.Text
lngCounter = Len(strNum)
Do Until lngCounter = 0
lngNum = 2 ^ lngCounter
strDecimal = strDecimal & lngNum
lngCounter = lngCounter - 1
Loop
End Function
now for some reason th emsg box comes back as (say if the decimal number is 513... converts it into 1000000001 the msg box will come back as 1024512256128643216842) how can i add thos enumbers without converting it into a long??? or do i have to?
I'm just a basic visual basic programmer!!! (no seriously... i'm just learning this stuff... the so called 'easy' stuff)... plz don't get mad at me if i don't know what i'm doing!!!
-
Mar 12th, 2004, 10:17 AM
#5
Thread Starter
Member
hmmm
VB Code:
Function BinaryToDecimal(ByVal strBin As String) As Long
Dim strNum As String
Dim lngNum As Long
Dim lngCounter As Long
Dim lngHelper As Long
strNum = txtNum.Text
lngCounter = Len(strNum) - 1
Do Until lngCounter < 0
lngNum = 2 ^ lngCounter
lngHelper = lngHelper + lngNum
lngCounter = lngCounter - 1
strDecimal = lngHelper
Loop
End Function
ok now it says if i convert 513 to binary and then i convert it back... i get an answer of 1023... help??? (i can't dl zip files on my school computer)
Last edited by MiddleAged1; Mar 12th, 2004 at 10:33 AM.
I'm just a basic visual basic programmer!!! (no seriously... i'm just learning this stuff... the so called 'easy' stuff)... plz don't get mad at me if i don't know what i'm doing!!!
-
Mar 12th, 2004, 10:40 AM
#6
Input binary and output binary ought to be stored as a string.
Given 10110101, StrReverse the string so you go left to right (1 to Len())
VB Code:
Option Explicit
Private Sub Command1_Click()
Text2.Text = BinaryToDecimal(Text1.Text)
End Sub
Private Sub Command2_Click()
Text3.Text = DecimalToBinary(Val(Text2.Text))
End Sub
Public Function BinaryToDecimal(strInputBits As String) As Long
Dim strRevBinaryBits As String
Dim lngDecimal As Long
Dim lngX As Long
strRevBinaryBits = StrReverse(strInputBits)
lngDecimal = 0
For lngX = 1 To Len(strRevBinaryBits)
If Val(Mid(strRevBinaryBits, lngX, 1)) > 0 Then
lngDecimal = lngDecimal + 2 ^ (lngX - 1)
End If
Next
BinaryToDecimal = lngDecimal
End Function
Public Function DecimalToBinary(lngInputLong As Long) As String
Dim strTempBits As String
strTempBits = ""
Do While lngInputLong > 0
strTempBits = strTempBits & (lngInputLong Mod 2)
lngInputLong = lngInputLong \ 2
Loop
DecimalToBinary = StrReverse(strTempBits)
End Function
Last edited by leinad31; Mar 12th, 2004 at 10:47 AM.
-
Mar 12th, 2004, 11:49 AM
#7
Just in case...
VB Code:
Public Function EnsureBinaryString(strInput As String) As String
Dim lngX As Long
Dim strTemp As String
strTemp = ""
For lngX = 1 To Len(strInput)
If Mid(strInput, lngX, 1) = "1" Or Mid(strInput, lngX, 1) = "0" Then
strTemp = strTemp & Mid(strInput, lngX, 1)
End If
Next
EnsureBinaryString = strTemp
End Function
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
|